'Bring UIButton to front layer

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,



Solution 1:[1]

I am late for this question, but not too late to answer it by Swift 3

Swift 3

 view.bringSubview(toFront: theButton)

Solution 2:[2]

A UIButton is a UIView, so you can call -bringSubviewToFront: on your button instance.

Solution 3:[3]

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)

Debug window tool bar

  • 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).

UI View Hierarchy

Solution 4:[4]

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)

Solution 5:[5]

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)  

Solution 6:[6]

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)
    }

Solution 7:[7]

button.superview?.bringSubviewToFront(button)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Gereon
Solution 3 halfer
Solution 4 Community
Solution 5 Dave Levy
Solution 6 dimaskpz
Solution 7 Sergey Osokin