Swift: 将 View 从 Stack View 移到前面
Swift: Bring View from Stack View to front
我得到了一个堆栈视图,视图为:1、2、3、4 和 5
正在寻找一种方法,将 Image View4 从 view4 引入 Stack 视图,置于所有其他视图之上。我需要这个的原因是因为我将 Image View4 移动到 view3 中的 Image View3 上。
我试图在视图层次结构中将 view3 移到 view4 上,但这只是交换了它们在堆栈视图中的位置。
使用 addSubview(_:)
从堆栈视图中删除图像并将其添加到包含堆栈视图的同一视图作为子视图。这将把它置于所有其他视图之上。您还可以使用 insertSubview(_:aboveSubview:)
将其直接插入堆栈视图上方。
您需要向新视图添加约束,使其位于您想要的位置。
The order of [the views in] the subviews
array defines the Z-order of the subviews. If the views overlap, subviews with a lower index appear behind subviews with a higher index.
因此,由于堆栈视图的 arrangedSubviews
数组中的所有视图也在堆栈视图的 subviews
数组中,您应该能够使用标准 UIView
API 来重新排列视图Z 顺序:
bringSubview(toFront:)
sendSubview(toBack:)
exchangeSubview(at:withSubviewAt:)
我不知道这是否可以在 Interface Builder 中完成。
您可以在 UIViewController 中使用 IBOutlet 属性,但在视图层次结构中它被放置在 UIStackView 中。
所以你应该使用
stackView.bringSubviewToFront(myView)
而不是
self.view.bringSubviewToFront(myView)
我得到了一个堆栈视图,视图为:1、2、3、4 和 5
正在寻找一种方法,将 Image View4 从 view4 引入 Stack 视图,置于所有其他视图之上。我需要这个的原因是因为我将 Image View4 移动到 view3 中的 Image View3 上。
我试图在视图层次结构中将 view3 移到 view4 上,但这只是交换了它们在堆栈视图中的位置。
使用 addSubview(_:)
从堆栈视图中删除图像并将其添加到包含堆栈视图的同一视图作为子视图。这将把它置于所有其他视图之上。您还可以使用 insertSubview(_:aboveSubview:)
将其直接插入堆栈视图上方。
您需要向新视图添加约束,使其位于您想要的位置。
The order of [the views in] the
subviews
array defines the Z-order of the subviews. If the views overlap, subviews with a lower index appear behind subviews with a higher index.
因此,由于堆栈视图的 arrangedSubviews
数组中的所有视图也在堆栈视图的 subviews
数组中,您应该能够使用标准 UIView
API 来重新排列视图Z 顺序:
bringSubview(toFront:)
sendSubview(toBack:)
exchangeSubview(at:withSubviewAt:)
我不知道这是否可以在 Interface Builder 中完成。
您可以在 UIViewController 中使用 IBOutlet 属性,但在视图层次结构中它被放置在 UIStackView 中。 所以你应该使用
stackView.bringSubviewToFront(myView)
而不是
self.view.bringSubviewToFront(myView)