Swift - UIViews 以编程方式排序
Swift - UIViews order programmatically
我在一个 ViewController 中有几个 UIView 层。
如何以编程方式确定 UIView 的顺序?
我找到了解决方案...
在 UIView 文档中,列出了几种用于操作子视图顺序的方法:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
在Swift 4.0
view.sendSubview(toBack:yourUIView)
view.bringSubview(toFront:yourUIView)
视图在 view.subviews
数组中从最后到最前面排序。
例如,这里有3个子视图被添加到一个空视图中。
view.addSubview(UILabel())
view.addSubview(UIButton())
view.addSubview(UITextField())
使用以下代码打印子视图
for view in view.subviews {
print(view.debugDescription)
}
有输出
<UILabel: 0x7feef00042e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400008c210>>
<UIButton: 0x7feef00045c0; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x604000222300>>
<UITextField: 0x7feeef02c800; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x60800005b090>; layer = <CALayer: 0x604000222280>>
重新排序视图后,view.subviews
中的位置会发生变化
if let button = view.subviews.first(where: { [=13=] is UIButton }) {
view.bringSubview(toFront: button)
}
现在,打印视图,得到输出
<UILabel: 0x7feef00042e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400008c210>>
<UITextField: 0x7feeef02c800; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x60800005b090>; layer = <CALayer: 0x604000222280>>
<UIButton: 0x7feef00045c0; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x604000222300>>
我在一个 ViewController 中有几个 UIView 层。
如何以编程方式确定 UIView 的顺序?
我找到了解决方案...
在 UIView 文档中,列出了几种用于操作子视图顺序的方法:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
在Swift 4.0
view.sendSubview(toBack:yourUIView)
view.bringSubview(toFront:yourUIView)
视图在 view.subviews
数组中从最后到最前面排序。
例如,这里有3个子视图被添加到一个空视图中。
view.addSubview(UILabel())
view.addSubview(UIButton())
view.addSubview(UITextField())
使用以下代码打印子视图
for view in view.subviews {
print(view.debugDescription)
}
有输出
<UILabel: 0x7feef00042e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400008c210>>
<UIButton: 0x7feef00045c0; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x604000222300>>
<UITextField: 0x7feeef02c800; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x60800005b090>; layer = <CALayer: 0x604000222280>>
重新排序视图后,view.subviews
中的位置会发生变化
if let button = view.subviews.first(where: { [=13=] is UIButton }) {
view.bringSubview(toFront: button)
}
现在,打印视图,得到输出
<UILabel: 0x7feef00042e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400008c210>>
<UITextField: 0x7feeef02c800; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x60800005b090>; layer = <CALayer: 0x604000222280>>
<UIButton: 0x7feef00045c0; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x604000222300>>