Search code examples
iosswiftuiviewcgrectuiview-hierarchy

Setting the priority of CGRect - Swift


Withing my iOS app, written in Swift, I have a view where i programmatically show a frame that takes up the space of the entire view. I have a button outlet within the story board. When the view loads, the button is hidden behind the frame.

view.frame =  CGRect(x: 0.0, y: 0.0, width: videoW, height: videoH)
self.view.addSubview(view)

How do I show the button over the frame?


Solution

  • Use insertSubview(atIndex:) to insert a subview at a particular location within the view hierarchy. Using index zero will place it underneath all other subviews:

    view.insertSubview(subview, atIndex: 0)
    

    Or if you have a reference to the button, you can insert this new subview directly below it:

    view.insertSubview(subview, belowSubview: button)