Prevent click through view in swift
Asked Answered
G

2

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.

Graupel answered 27/7, 2016 at 15:42 Comment(0)
K
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.

Kennethkennett answered 27/7, 2016 at 15:58 Comment(4)
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.Graupel
@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 falseMycology
@Mycology 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.Graupel
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.Mycology
C
9

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

Casas answered 21/5, 2017 at 14:6 Comment(0)
K
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.

Kennethkennett answered 27/7, 2016 at 15:58 Comment(4)
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.Graupel
@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 falseMycology
@Mycology 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.Graupel
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.Mycology

© 2022 - 2024 — McMap. All rights reserved.