Lennart P.
Lennart P.

Reputation: 376

Buttons are unclickable swift


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.

Upvotes: 0

Views: 415

Answers (2)

Lennart P.
Lennart P.

Reputation: 376

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

Upvotes: 0

Pranav Kasetti
Pranav Kasetti

Reputation: 9915

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)
    })
  }
}

Upvotes: 1

Related Questions