scrollView 的底部内容没有响应触摸事件

Posted

技术标签:

【中文标题】scrollView 的底部内容没有响应触摸事件【英文标题】:The bottom content of the scrollView is not not responding to touch events 【发布时间】:2017-02-06 06:42:03 【问题描述】:

我有滚动视图。在侧滚动视图中,我有一个带有尾随、前导、顶部和底部约束以及相等高度的容器视图。我正在动态添加视图。我在

中更新了内容大小

viewWillLayoutSubviews如下。

override func viewWillLayoutSubviews() 
        self.scrollView.contentSize = CGSize(width: self.contentView.frame.width, height: self.contentView.frame.height)
        self.contentView.frame = CGRect(x: self.contentView.frame.minX, y: self.contentView.frame.minY, width: self.contentView.frame.width, height: 2500) // code to update the contentView's frame where 2500 is a dummy value
        self.contentView.clipsToBounds = true
    

现在我可以滚动到容器视图的最后一个子视图。但问题是最后 3 个子视图没有响应触摸事件。

我尝试了以下方法,但都失败了。

还更新了containerView的框架 还更新了滚动视图的框架

没用。 请帮帮我。

【问题讨论】:

使用容器视图检查底部 3 视图的框架。 最后 3 个子视图没有响应触摸事件 -> 确保 isUserInteractionEnabled = true isUserInteractionEnabled = true 。它已启用。我已经检查过了。@NhatDinh 问题在于您的 contentView 高度,我同意您的滚动视图滚动到底部,您可以在底部看到子视图,但由于 contentView 高度而出现此问题。尝试为 UIScrollView 的 contentView 设置硬编码高度约束。 我会在 viewWillLayoutSubview 中放置一个断点并检查框架。它显示 (0,0,0,0) 但在模拟器中可见。 @RahulPatel 【参考方案1】:
 func updateFrame() 
            self.scrollView.contentSize = CGSize(width: self.contentView.frame.width, height: self.contentView.frame.height)
    self.contentView.translatesAutoresizingMaskIntoConstraints = false
            self.contentView.frame = CGRect(x: self.contentView.frame.minX, y: self.contentView.frame.minY, width: self.contentView.frame.width, height: 2500) // code to update the contentView's frame where 2500 is a dummy value
            self.contentView.clipsToBounds = true
        

试试这个。 我假设您的滚动视图大于内容视图

【讨论】:

代替viewWillLayoutSubviews,手动调用updateFrame这个方法 使用 Debug View Hierarchy 检查 contentView 的框架是否真的在更新? 里面什么都没有。但我已将它们添加到 viewDidLoad() 哥们,从来没有在视图中设置框架确实加载,它不会受到影响,或者如果你想在视图加载中更新框架,请使用 dispatchasynch 主队列调用它。你知道调试视图层次结构吗? 让我们continue this discussion in chat.

以上是关于scrollView 的底部内容没有响应触摸事件的主要内容,如果未能解决你的问题,请参考以下文章