Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent click through view in swift

I'm working in Xcode and swift, I created a view acting as a menu that toggles on tap, when the menu comes out I can still click a test button underneath it. I don't want that to happen. I want everything behind the view to be disabled giving priority to the menu view. (View Image below)

screenshot from the sample app

  • Keep in mind that I'm not considering one button, if that was the case I would've disabled that specific button. This page will be a scroll view and it will be dynamic.

this is the code that I'm using:

@IBAction func MenuButton(sender: UIButton) {
    if self.MenuView.frame.origin.x == -180 {
        UIView.animateWithDuration(0.5, animations:{
            self.MenuView.frame = CGRectMake(self.MenuView.frame.origin.x + 180, self.MenuView.frame.origin.y, self.MenuView.frame.size.width, self.MenuView.frame.size.height)
        })

    } else {
        UIView.animateWithDuration(0.5, animations:{
            self.MenuView.frame = CGRectMake(self.MenuView.frame.origin.x - 180, self.MenuView.frame.origin.y, self.MenuView.frame.size.width, self.MenuView.frame.size.height)
        })
    }
}

the view is hidden 180 pixels on the left side, when the menu button is clicked the view will animate 180 pixels to the right which brings it to the front. The function checks if the view is already opened so it can animate it back 180 pixel to hide it.

The only thing I need is to disable clicking through the view.

like image 219
Yousif Al-Raheem Avatar asked Jul 27 '16 15:07

Yousif Al-Raheem


2 Answers

Swift 3 version that worked great for me in stopping the click through (thanks to the previous comment by @Ismail).

myAddedSubView.isUserInteractionEnabled = true

This is the 'simple, one-line' answer and works like z index (presuming you wanted the z index to be 'absolute top'.

like image 99
Apps-n-Add-Ons Avatar answered Oct 27 '22 18:10

Apps-n-Add-Ons


The fact that the button is still visible and clickable means that it must be in front of the menu. If you rearrange the order of things so that the menu is in front of the button, then you should get the result that you are looking for.

like image 27
Westside Avatar answered Oct 27 '22 16:10

Westside