我有一个UICollectionViewCell子类,上面有UIStackView。 StackView填充了UIButtons,一切看起来都很好 但是这些按钮是不可用的。
如果我在UIScrollView上设置相同的按钮(为了实验)而不是在堆栈视图上,按钮响应很好,所以看起来堆栈视图的某些东西导致问题
有什么想法吗?
以下是我如何向StackView添加按钮的代码
func prepareStackView(buttonsArray: Array<UIButton>) {
var rect:CGRect = (stackView?.frame)!
rect.size.width = btn.frame.size.width * buttonsArray.count
stackView?.frame = rect //The frame of the stackview is set as such so that it looks exactly like I want
//Add all the buttons
for btn in buttonsArray {
//The buttons already have their selectors set
stackView?.addArrangedSubview(btn)
}
}
答案 0 :(得分:0)
将此代码用于UIButton以响应点击事件:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if clipsToBounds || isHidden || alpha == 0 {
return nil
}
for subview in subviews.reversed() {
let subPoint = subview.convert(point, from: self)
if let result = subview.hitTest(subPoint, with: event) {
return result
}
}
return nil
}
有用的链接:
Capturing touches on a subview outside the frame of its superview using hitTest:withEvent:
我也遇到过类似的问题而且能够解决。请查看我的帖子,我已经描述了这种原因的情景,并试图解释我是如何解决的。