0


I've got a problem with two of my buttons in my Xcode project. After adding two views above them to create a border around them they cannot be clicked anymore. After removing them again the buttons work but adding the back causes them to not work.

Here's my view hierarchy in my project:

Before:

-mainView
 -scrollView
  -contentView
   -stackView
    -stackView
     -stackView
      -stackView
       -Button1
       -Button2

After:

-mainView
 -scrollView
  -contentView
   -stackView
    -stackView
     -stackView
      -stackView
       -uiView (new View)
        -uiView (new View)
         -Button1
       -uiView (new View)
        -uiView (new View)
         -Button2

Is it because there are too many views?

Thank you very much, if you need any more pieces of information just write a comment and I'll try to help in the next edit.

5
  • Are you sure there is not a view on top of the button? Commented Sep 9, 2017 at 14:19
  • @Shades No, both of them are inside of the views like I displayed it above. Commented Sep 9, 2017 at 22:04
  • Check that user interaction is enabled for those views Commented Sep 9, 2017 at 22:11
  • It already was... Commented Sep 9, 2017 at 22:21
  • I'm out of ideas. Usually this problem is either caused by something (including UIViews and Stackviews, and of course the button itself) having interaction disabled, or the button not actually being in the hierarchy so the view is on top of it. Commented Sep 9, 2017 at 22:25

2 Answers 2

1

Subclass UIView as a PassthroughView and set the class of your newly added views to be PassthroughView. Add this code:

class PassthroughView: UIView {
  override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    return subviews.contains(where: { !$0.isHidden && $0.point(inside: point, with: event)
    })
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What does this do?
It passes the touch through the superview of each button.
Unfortunately, it doesn't work but I might have found another way so that I won't need to use a button at all but by using one of the views as a button with a Tap Gesture Recognizer.
0

Okay, I've got the solution now... It worked after setting the Alignment and Distribution of the StackView to Fill

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.