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

问题描述 投票:8回答:7

UISplitView的detailViewController中,我将子视图添加到UIStackView中的UIScrollView

只使用没有subviewsimages的系统按钮,会产生响应按钮,但subviews似乎会干扰。

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

谢谢你的帮助。

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

}
swift uibutton scrollview uistackview
7个回答
4
投票

看起来有些东西在你的按钮上趴着并抓住所有的触摸事件。

1.如果您不需要,请勿打开userInteractionEnabled

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

2.找到错误:

要查找女巫视图捕获事件,请在设备上启动应用程序并导航到stackView。现在在Xcode中,按下“Debug View Hierarchy”按钮(放在控制台输出上方)

enter image description here

之后,您将看到设备上当前显示的每个视图。你现在做的是发现你的按钮上面有任何视图,然后在代码中将其userInteractionEnabled值变为false。


18
投票

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

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

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


5
投票

在我的例子中,StackView的aligment属性导致了问题。

当我将该属性设置为qazxsw poi而不是qazxsw poi时,问题就消失了。


1
投票

在thegy的Fill之后,我使用了Center,我立即发现了问题:

answer


0
投票

我遇到了同样的问题。原因是我设置button.alpha = 0,同时设置button.layer.opacity = 1.0。在这种情况下,按钮不会响应操作,尽管它是可见的。所以设置button.alpha = 1.0就是答案。可能有帮助!


0
投票

在我的例子中,我在sherlock app中设置了一个自定义视图子类。该子类有enter image description here,它转换为所有子视图,包括我的stackView及其所有视图和按钮。


0
投票

我自己也遇到过这个问题。在我的例子中,我有一个UIStackView作为我的UIButton的子视图。我需要在堆栈视图上设置loadView才能使我的按钮工作。在任何UIButton子视图上设置这可能是明智之举。

© www.soinside.com 2019 - 2024. All rights reserved.