Reputation: 3823
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,
Upvotes: 16
Views: 28180
Reputation: 87
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)
}
Upvotes: 0
Reputation: 1172
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)
Upvotes: 1
Reputation: 6002
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
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).Upvotes: 6
Reputation:
I am late for this question, but not too late to answer it by Swift 3
view.bringSubview(toFront: theButton)
Upvotes: 13
Reputation: 9350
The following works great if the view you are working with is being hidden views that are in the same view.
superview?.bringSubviewToFront(self)
Upvotes: 2
Reputation: 17844
A UIButton is a UIView, so you can call -bringSubviewToFront:
on your button instance.
Upvotes: 6