I know there is a way of bringing a subview to the front of the hierarchy. However is there a way of bringing a button that i have placed on a story and calling a function that will make that button the top layer so its not hidden by any other elements that are added that aren't in the story board.
Thanks,
I am late for this question, but not too late to answer it by Swift 3
Swift 3
view.bringSubview(toFront: theButton)
A UIButton is a UIView, so you can call -bringSubviewToFront:
on your button instance.
uiview
's are displayed in the order they are stacked. One can change this to say were to insert the new uiview
, which in your case is a uibutton.
One way of doing that is by creating a IBOUtlet
of the uibutton
and adding that button instance as a subview/newview above an existing uiview
.
CODE:
[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];
Also if you are not able to figure out the order of the uiview
, then you can check that in xcode
- When running the project in debug mode under debug window there is a button(highlighted in below image) Debug View Hierarchy, you need to click that. (highlighted in below image)
![](https://www.manongdao.com/static/images/pcload.jpg)
- After that you will able to see all the
views
rendering on the screen at current instance, using this feature you will be able to understand were is your new uiview
in the uiview stack order
(shown below).
![](https://www.manongdao.com/static/images/pcload.jpg)
Swift
The following works great if the view you are working with is being hidden views that are in the same view.
superview?.bringSubviewToFront(self)
Swift 5
In my case I had to insert my view behind some buttons. By insert it at the zero position it was placed behind the buttons.
self.view.insertSubview(self.mapView!, at: 0)
i have the same problem with you and a friend of mine helped me
i can bring my button to another subview using this function on your view controller where you declare your button.
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.keyWindow?.addSubview(chatButton)
}