子视图中无响应的 UIButton 添加到 UIStackView [英] Unresponsive UIButton in subview added to UIStackView
问题描述
在 UISplitView
的 detailViewController 中,我将子视图添加到 UIScrollView
内的 UIStackView
.
In the detailViewController of a UISplitView
I have subviews added to a UIStackView
inside a UIScrollView
.
仅使用没有 subviews
或 images
的系统按钮,会产生响应式按钮,但 subviews
似乎会干扰.
Just using system buttons without subviews
or images
, results in responsive buttons, but subviews
seem to interfere.
启用触摸是专门编码的.我试图将每个视图保留在包含视图中,因此不会有重叠使接收触摸事件无效,但不确定这是否正确完成.每个 subview
包含一个 label
和一个带有图像的自定义按钮.然后将子视图添加到 stackview
,将 stackview
添加到 scrollview
.
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
.
感谢您的帮助.
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.如果您不需要,请不要开启 userInterectionEnabled
没有理由像 subView 这样的普通视图应该将 userInteractionEnabled 设置为 true,因此将其设置为 false.
There is no reason why a normal view like subView should have set userInteractionEnabled set to true so put it to false.
2.找到错误:
要找出女巫视图捕获的事件,请在设备上启动您的应用程序并导航到 stackView.现在在 Xcode 中,按下按钮Debug View Hierarchy"(位于控制台输出的正上方)
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)
之后,您将看到设备上当前显示的每个视图.您现在要做的是找出按钮上方的视图,然后在代码中将它们的 userInteractionEnabled 值设为 false.
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.
这篇关于子视图中无响应的 UIButton 添加到 UIStackView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!