Search code examples
swiftuicontainerview

Disable user interaction in some part of screen


I create side menu via container view and when user tapped on bar button ( menu button ) I just change constraints and show or hide side menu. But when side menu open user can use elements in all screen. How I can disable or enable only part of screen when side menu open or hide?


Solution

  • Suppose your side menu's view hierarchy is setup like this:

    View #1
     |_ title label
     |_ button
     |_ button
    

    You can now embed it inside another view, that'll be invisible:

    View #0
     |_ View #1
         |_ title label
         |_ button
         |_ button
    

    ...where view #1 would be your regular side menu view that covers, say, 70% of the screen's width.
    Now, set the view #0's background color to .clear.
    Also, change your constraint logic to move the side menu to the screen's edge.

    This will give you a side menu that covers the full screen, disables taps outside it and looks like it only covers part of the screen.


    Extra Credit:

    Add a UITapGestureRecognizer to view #0. When triggered, you can dismiss the side menu.