子视图中无响应的UIButton添加到UIStackView

Jol*_*lly 12 scrollview uibutton swift uistackview

在detailViewController中,UISplitView我有一个子视图添加到一个UIStackView内部UIScrollView.

只使用不带subviews或的系统按钮images,会产生响应按钮,但subviews似乎会干扰.

启用触摸是专门编码的.我试图将每个视图保留在包含视图中,这样就没有重叠来使接收触摸事件无效,但不确定这是否正确完成.每个subview包含一个label带图像的自定义按钮.然后,将子视图添加到stackview,和stackviewscrollview.

谢谢你的帮助.

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

}
Run Code Online (Sandbox Code Playgroud)

the*_*guy 20

我有完全相同的问题:包含视图的StackView.每个视图都包含一个按钮和一个标签.视图仅是出于布局原因的容器.

在我的情况下:我没有给视图一个高度约束,似乎它有一个零高度(在更改调试后的背景颜色后我没有看到视图),但按钮和标签是可见的(但没有响应).

在向布局视图添加高度约束后,(调试)背景颜色可见,按钮按预期响应.


jma*_*nso 9

以我aligment为例,正是导致该问题的StackView属性。

当我将该属性设置为Fill而不出现Center问题时。


Kyl*_*nin 8

我自己刚刚遇到了这个问题。就我而言,我有一个 UIStackView 作为 UIButton 的子视图。我需要isUserInteractionEnabled = false在堆栈视图上进行设置才能使我的按钮工作。这可能是在任何 UIButton 子视图上设置的明智之举。

  • 我遇到了完全相反的问题,我必须在 UIStackView 上设置 `isUserInteractionEnabled = true` 才能让按钮注册点击。这是我以编程方式创建的 UIStackView。 (4认同)

M. *_*mer 6

似乎是您的按钮上藏着一些东西,并捕获了所有触摸事件。

1.如果不需要,请不要打开userInterectionEnabled

没有理由为什么像subView这样的普通视图应该将userInteractionEnabled设置为true,因此将其设置为false。

2.查找错误:

要找出女巫视图捕获事件,请在设备上启动您的应用程序,然后导航到stackView。现在,在Xcode中,按下“调试视图层次结构”按钮(位于控制台输出的正上方)

在此处输入图片说明

之后,您将看到设备上当前显示的每个视图。现在,您要做的是找出位于按钮上方的视图,然后在代码中将其userInteractionEnabled值设置为false。