Fudgey
Fudgey

Reputation: 3823

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,

Upvotes: 16

Views: 28180

Answers (7)

Sergei O
Sergei O

Reputation: 11

button.superview?.bringSubviewToFront(button)

Upvotes: 0

dimaskpz
dimaskpz

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

Dave Levy
Dave Levy

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

dreamweiver
dreamweiver

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

  • 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

Upvotes: 6

user1528293
user1528293

Reputation:

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

Swift 3

 view.bringSubview(toFront: theButton)

Upvotes: 13

user160917
user160917

Reputation: 9350

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)

Upvotes: 2

Gereon
Gereon

Reputation: 17844

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

Upvotes: 6

Related Questions