How to add a view behind other view in iOS
Besides addSubview
, there are other methods you can rely on to add your view:
– insertSubview:aboveSubview:– insertSubview:belowSubview:– insertSubview:atIndex:
You can control the index (actually z-index, or layer order) of the view using these methods.
Swift 5
Insert a view below another view in the view hierarchy:
view.insertSubview(subview1, belowSubview: subview2)
Insert a view above another view in the view hierarchy:
view.insertSubview(subview1, aboveSubview: subview2)
Insert a subview at the specified index:
view.insertSubview(subview, at: index)
Move the specified subview so that it appears behind its siblings:
subview1.sendSubviewToBack(subview2)
Move the specified subview so that it appears on top of its siblings:
subview1.bringSubviewToFront(subview2)
Exchange the subviews at the specified indices:
view.exchangeSubview(at: index1, withSubviewAt: index2)