How can I bring a view in front of another view, in Swift?

前端 未结 5 745
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 10:37

Below are how my views are organized in IB, top->bottom when the app is started.

The user can do something to make \"Category Table View Header\" temporarily expand

相关标签:
5条回答
  • 2020-12-16 11:09

    The method has been updated since swift 3

    What has worked for me and hopefully for you is using :

    YourView.bringSubview(toFront: yourelementA)
    YourView.bringSubview(toFront: yourelementB)
    

    Alternatively you could use the following to send the object that is in the front:

    YourView.sendSubview(toBack: yourelementC)
    

    I hope this helps

    0 讨论(0)
  • 2020-12-16 11:11

    In Swift 5

    To bring a subview to front

    self.view.bringSubviewToFront(yourView)
    

    To send a subview to back

    self.view.sendSubviewToBack(yourView)
    
    0 讨论(0)
  • 2020-12-16 11:17

    If you want to bring a subview to the front, you can use:

    SWIFT 4 UPDATE

    self.view.bringSubviewToFront(yourView)
    

    SWIFT 3 UPDATE

    self.view.bringSubview(toFront: yourView)
    

    Send view to back:-

    SWIFT 4 UPDATE

    self.view.sendSubviewToBack(yourView)
    

    SWIFT 3 UPDATE

    self.view.sendSubview(toBack: yourView)
    
    0 讨论(0)
  • 2020-12-16 11:20

    Swift 5.1+

    UIKit draws views back to front, which means that views higher up the stack are drawn on top of those lower down. If you want to bring a subview to the front, there's a method just for you: bringSubviewToFront(). Here's an example:

    parentView.bringSubviewToFront(childView)
    

    This method can also be used to bring any subview to the front, even if you're not sure where it is:

    childView.superview?.bringSubviewToFront(childView)
    
    0 讨论(0)
  • 2020-12-16 11:28

    You can take a control over the order of subviews using methods: bringSubviewToFront and sendSubviewToBack from the superview.

    You can access all the subviews contained by superview using self.view.subview array.

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