我正在撞墙。我有一个带有子 UIView (contentView) 的 UIScrollView,其中包含内容和一个按钮。该按钮未检测到任何点击。我已经测试了很多方法,以编程方式创建按钮,使用 Storyboard,并在 contentView 中创建 UITapGestureRecognizer ......仍然没有。据我了解, UIScrollView 不会将任何触摸事件委托(delegate)给它的 child 。我该如何解决这个问题?
最佳答案
您可以通过编程方式创建按钮,然后将其添加为您的 contentView 的 subview 。
let button = UIButton()
button.frame = CGRectMake(contentView.center.x-20.0, contentView.center.y-20.0, 40.0, 40.0)
button.addTarget(self, action: "handleTap", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("Title", forState: UIControlState.Normal)
contentView.addSubview(button)
关于你可以处理点击的 Action
func handleTap() {
//Do whatever you want
print("Button tapped")
}
我使用 scrollView 和 scrollView 中的 UIView 对其进行了测试
关于ios - 带有子 UIView(包含内容)的 UIScrollView 未检测到点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35020032/