Noo*_*dew 2 uiscrollview uiview uigesturerecognizer ios swift
我将所有 UIElement 移动到滚动视图中以避免加载 UIViewcontroller。但现在视图上的 GestureRecognizers 不再起作用了。
我的视图层次结构如下所示:
- UIViewController(单事件控制器)
- UIScrollView (EventScrollView)
- UIView(内容视图)
- UILabel、UIView、UITableView 等
这看起来是一个常见问题,因为我在 Stackoverflow 上发现了很多类似的问题: 示例 1 示例 2。尽管如此,我还是没能解决我的案子。
我的EventScrollView中的简化代码如下所示:
let locationLabel: UILabel = {
let label = UILabel()
label.text = "Standort"
label.isUserInteractionEnabled = true //Important!
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true //not needed!
contentView.isUserInteractionEnabled = true //Important!
}
func setupViews() {
guard let parent = parentVC else { return }
let locationTap = UITapGestureRecognizer(target: parent, action: #selector(parent.openInGoogleMaps))
locationTap.cancelsTouchesInView = false //Important!
addSubview(contentView)
contentView.addSubview(locationLabel)
locationLabel.addGestureRecognizer(locationTap)
}
Run Code Online (Sandbox Code Playgroud)
我缺少哪一步?顺便说一句,单击contentViewUITableView内的一行也不会被注册。
我在这个答案的评论中发现了我的问题。 我的scrollView 中的contentView 包含所有其他视图,其高度为0,依此类推,无法单击。通过给视图一个高度,它起作用了:
contentView.heightAnchor.constraint(equalTo: self.frameLayoutGuide.heightAnchor, multiplier: 1).isActive = true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1670 次 |
| 最近记录: |