6

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.

Yousif Al-Raheem
  • 452
  • 5
  • 15

2 Answers2

8

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'.

Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28
1

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.

Westside
  • 675
  • 1
  • 7
  • 16
  • Thanks, that solved it. I'm new to swift. I thought bringing a view (Div) in front of another element will disable it. I don't know why Apple had to make it that complex. Thanks anyway. – Yousif Al-Raheem Jul 27 '16 at 16:03
  • 1
    @YousifAl-Raheem There a case that the view will pass the touch to the button even though it's under the view. This case when the view `User Interaction Enabled` is `false` – Ismail Jul 27 '16 at 16:07
  • @Ismail I tried that, setting user interaction enabled to false will disabled the menu which makes it useless. Rearranging it in the storyboard is better, I thought there will be a code solution, for example: one line code to set the view z index. – Yousif Al-Raheem Jul 27 '16 at 16:46
  • That's not what I meant. I meant it should be on because if it's off it will cause the buttons underneath will be clickable through it. – Ismail Jul 27 '16 at 16:58