How do I change the z index or stack order of UIView?

前端 未结 2 1203
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 10:59

I\'ve stacked a UIView on top of an existing controller in an effort to create a gradient layer. However, when I load the view the new UIView (naturally) is on top of everyt

相关标签:
2条回答
  • 2020-12-24 11:42

    the above answer of

    bringSubviewToFront(_:) sendSubviewToBack(_:)

    has changed:

    In Swift 3.0

    view.sendSubview(toBack:yourUIView)
    view.bringSubview(toFront:yourUIView)
    

    In Swift 4.0 && Swift 5.0

    view.sendSubviewToBack(yourUIView)
    view.bringSubviewToFront(yourUIView)
    
    0 讨论(0)
  • 2020-12-24 11:51

    I would recommend looking in the UIView documentation, where are several methods listed for the manipulation of the order of subviews:

    bringSubviewToFront(_:)
    sendSubviewToBack(_:)
    removeFromSuperview()
    insertSubview(_:atIndex:)
    insertSubview(_:aboveSubview:)
    insertSubview(_:belowSubview:)
    exchangeSubviewAtIndex(_:withSubviewAtIndex:)
    

    In your situation, you could try:

    self.view.sendSubviewToBack(myGradientView)
    // self is your view controller in this case.
    

    Alternatively, because you created the myGradientView in IB, you could change the view hierarchy to change which views appeared on top. Here's a picture to illustrate:

    enter image description here

    Hope that helps.

    0 讨论(0)
提交回复
热议问题