Unresponsive UIButton in subview added to UIStackView

uibutton not responding to touch swift
add stackview programmatically swift
uistackview tap gesture
add arranged subview not working
stackview in xib
uistackview dynamic height
uistackview divider
uistackview alignment

In the detailViewController of a UISplitView I have subviews added to a UIStackView inside a UIScrollView.

Just using system buttons without subviews or images, results in responsive buttons, but subviews seem to interfere.

Enabling touch is specifically coded. I have attempted to keep each view inside the containing view so there will be no overlap to invalidate receiving touch events, but not sure if this is done properly. Each subview contains a label and a custom button with an image. The subview is then added to the stackview, and the stackview to the scrollview.

Thanks for any help.

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(scrollView)

    // Constrain the scroll view within the detailView
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))


    stackView = UIStackView()

    stackView.frame = CGRectMake(0,0,view.frame.width, 1000)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .Vertical
    scrollView.contentSize = CGSizeMake(400, 1000)
    scrollView.addSubview(stackView)

    // Constrain the stackView within the scrollView
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[stackView]|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[stackView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))


    let selectedGroup: Group = GroupArray[5]

    let descriptorsArray = selectedGroup.descriptorsArray

    for descriptor in descriptorsArray {
        // Create a subview for each descriptor

        let subView = UIView()
        subView.frame = CGRectMake(0 , 0, self.stackView.frame.width-10, 54)
        subView.backgroundColor = UIColor.yellowColor()

        subView.heightAnchor.constraintEqualToConstant(54.0).active = true
        // Create a label for Descriptor subview

        let label = UILabel(frame: CGRectMake(20, 0, 200, 50))
        label.text = descriptor.name
        label.font = UIFont.boldSystemFontOfSize(22.0)
        label.textAlignment = .Left
        label.textColor = UIColor.brownColor()
        label.backgroundColor = UIColor.greenColor()
        label.heightAnchor.constraintEqualToConstant(50.0).active = true
        subView.addSubview(label)

        // Create a button for Checkbox
        let btn = UIButton()
        btn.frame = CGRectMake(220, 0, 50, 50)
        btn.backgroundColor = UIColor.blueColor()
        btn.setImage(UIImage(named:"checked.png"), forState: UIControlState.Normal)

        btn.heightAnchor.constraintLessThanOrEqualToConstant(50.0)
        btn.widthAnchor.constraintLessThanOrEqualToConstant(50.0)

        btn.addTarget(self, action: "btnPressed:", forControlEvents: UIControlEvents.TouchUpInside)

        subView.addSubview(btn)
        btn.userInteractionEnabled = true
        subView.userInteractionEnabled = true
        stackView.userInteractionEnabled = true
        scrollView.userInteractionEnabled = true
        stackView.addArrangedSubview(subView)


    }
}

func btnPressed(sender: UIButton!) {

    print("btn Pressed")

}

It seems to be that something is lying over your button and catching all the touch events.

1. Never turn on userInterectionEnabled if you don't need it

There is no reason why a normal view like subView should have set userInteractionEnabled set to true so put it to false.

2. Find the bug:

To find out witch view catches the event start your app on a device and navigate to the stackView. Now in Xcode, press the button "Debug View Hierarchy" (placed right over the console output)

After that you will see every view currently displayed on your device. What you do now is finding out wich views are above your button, and then in code turn their userInteractionEnabled value to false.

swift, It seems to be that something is lying over your button and catching all the touch events. 1. Never turn on userInterectionEnabled if you don't need it. There is no  24 Unresponsive UIButton in subview added to UIStackView Oct 10 '17. 15 use xcassets without imageNamed to prevent memory problems? May 2 '14.

I have had the exact same issue: a StackView which contains views. Each view contains a button and a label. The views are only containers for layout reasons.

In my case: I did not give the view a height constraint and it seems that it had a zero height (after changing the background color for debugging I did not see the view), but the button and label was visible (but unresponsive).

After I have added a height constraint to the layout view the (debug) background color was visible and the buttons responded as expected.

Adding views to UIStackView with addArrangedSubview(), In the detailViewController of a UISplitView I have subviews added to a UIStackView inside a UIScrollView. Adding views to UIStackView with addArrangedSubview() With our storyboard designed, it's time to write the code. As you know, our plan is to produce an app where the user can have multiple web views visible at one time, stacked together and usable in their own right.

In my case, it was the aligment property of the StackView that was causing the problem.

When I set that property to Fill instead of Center the problem dissappeared.

UiButton ClickEvents are not working when I am add UiButton to the , Instead, you add to its arranged subviews array, and the stack view will arrange them as needed. So, our first draft of addWebView() is pretty easy: we create a new  However, you don’t want the height of the Hide button to dictate the height of the stack view. Instead, you’ll eliminate the Hide button from all stack views. The Hide button will remain a subview of the top-level view, and you’ll add a constraint from it to the WEATHER label — which will be in a stack view.

In my case, I had set a custom view subclass in loadView. That subclass had isUserInteractionEnabled = false which translated down to all the sub views including my stackView and all its views and buttons.

Back to the UIStackView: The tale of the incredible , UiButton ClickEvents are not working when I am add UiButton to the StackView as a 'SubView'. gdk INMember. December 2016 in Xamarin.iOS. These are  The space that is allocated to each subview is determined by a number of factors including the subview's intrinsic content size and it's compression resistance and content hugging priorities. By default, UIView instances don't define an intrinsic content size. This is something that is generally provided by a subclass, such as UILabel or UIButton.

following theGuy's answer, I used the sherlock app and I immediately found the problem:

UIButton not responding to touch events, Create a UIButton and set the title text and colour: regular view, but now try adding that button to a UIStackView by first creating an extension A solution could be to add another UIStackView inside your original UIStackView and give the leading to this new UIStackVIew. Then, add your views to this new UIStackView. Hint, you can do that completely using Interface Builder. In other words, no need to write code for it.

Touches not passed to UIStackView arranged subviews : swift, I have a button that is showing but not responding to touches. addPlayButton() } func addPlayButton() // Adds the "Play" button { // Add button  I would like to add a UIView which has 3 UIButtons in a UIStackView. The reason is that I would like to make this UIView transparent to the screen by altering its alpha. As far as I experienced, I see that I can add only one UIView which has just one subView (e.g. UIButton, UILabel etc) in a StackView avoiding problems of layout.

UIButton does not respond to event inside a UIBarButtonItem's , Touches not passed to UIStackView arranged subviews I would like to have a UIButton but for debug purposes I have tried using both touchesBegan It is still nascent but I've planned a list of features that I would be adding to it soon, given  We have already discussed through examples and implemented UIStackView in a tutorial before. In this tutorial, we’ll learn how to add a UIStackView inside a UIScrollView and also implement adding and removing views from a UIStackView programmatically and animate them.

Button not working in UIScrollView |Apple Developer Forums, UIButton does not respond to event inside a UIBarButtonItem's custom view. Solved! Calling sizeToFit() on a UIButton instance before adding it to a superview  I'm fairly new to coding for the iOS platform and objective C in general. I'm writing a simple iPhone app utilizing storyboard that has a tall png file displayed via UIImageView embedded within a UIScrollView and a button next to it that will play a movie. My issue is that when the movie finishes/e

Comments
  • M Kremer thank you so much for this debugging suggestion. It is the correct textbook and functional approach so I marked it as correct, but I failed to find the overlap using this approach. Since I knew stacked buttons worked while stacked subviews didn't (for me), my solution was to stack buttons instead of subviews with buttons in them. I changed the background of each button depending on state. Instead of a label, I set the title. No views to overlap as all should be contained in the button.
  • The default value for all views is YES.
  • This saved me, thanks. I added a missing label.bottomAnchor.constraint(...) and everything started magically working again.
  • I had the similar issue. I didn't set width constraint to the wrapper view so it has zero width in runtime.
  • Thank you for this! I had tried everything I could think of and I would never have thought to try that!
  • Thank you, Its help me.
  • It's this kind of stuff that UIKit does that makes me want to through my Mac through a window. Thanks for this answer!