gpt4 book ai didi

swift - subview 中无响应的 UIButton 添加到 UIStackView

转载 作者:IT王子 更新时间:2023-10-29 05:39:01 24 4
gpt4 key购买 nike

UISplitView 的 detailViewController 中,我将 subview 添加到 UIScrollView 内的 UIStackView

仅使用不带 subviewsimages 的系统按钮会产生响应式按钮,但 subviews 似乎会产生干扰。

启用触摸是专门编码的。我试图将每个 View 保留在包含 View 中,因此不会有重叠以使接收触摸事件无效,但不确定是否正确完成。每个 subview 都包含一个 label 和一个带有图像的自定义按钮。然后将 subview 添加到 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")

}

最佳答案

我遇到了完全相同的问题:包含 View 的 StackView。每个 View 都包含一个按钮和一个标签。出于布局原因, View 只是容器。

在我的例子中:我没有给 View 一个高度约束,它似乎有一个零高度(在更改背景颜色进行调试后我没有看到 View ),但是按钮和标签是可见的(但是 react 迟钝)。

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

关于swift - subview 中无响应的 UIButton 添加到 UIStackView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39167337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com