Search code examples
iosswiftxcodecocoa-touchuiview

Swift bringSubViewToFront not working


I have a number of labels and buttons on the screen. To act on user tapping anywhere on the screen, I have created a UIButton ("wholeScreenButton"), made its background clear and made it the same size as superview. Now at runtime I am using below code to bring the button on top of every other label.

self.wholeScreenButton.bringSubviewToFront(self.wholeScreenButton)

But it is not working. User can still tap on some of the buttons which are supposed to be underneath the wholeScreenButton and supposed to be unreachable. What am I doing wrong?


Solution

  • It should be this:

    self.view.bringSubview(toFront: self.wholeScreenButton)
    

    You need to call bringSubviewToFront on the parent view.