【问题标题】:UIButton not responsive on it whole surface (UICollectionViewCell)UIButton 在整个表面上没有响应(UICollectionViewCell)
【发布时间】:2016-10-13 03:29:12
【问题描述】:

问题:

我在链接到 IBAction 的 UICollectionViewCell 中有一个 UIButton。屏幕截图上的按钮为黄色 (screenshot here)。 问题是按钮仅在我们触摸自身的特定部分而不是整个区域时才调用动作;从按钮高度和整个宽度的底部大约 20 像素的区域。 (我把这个区域画成红色here

我的项目结构:

我正在使用 UICollectionView 一个接一个地向用户提出一些问题。用户可以通过触摸按钮、在地图上移动、选择日期等来回答这些问题……每个问题都有相同的布局;一个标题标签、一个按钮和一个包含回答问题工具的视图。这就是我选择使用 UICollection 视图的原因。

对于每个问题,我在单元格中心的 UIView 中加载一个特定的视图(来自 XIB 文件)(屏幕截图上的绿色)。每个 XIB 文件的 File's Owner 被设置为一个名为 @IBDesignable class typeQuestionVC: UIView {...} 的类。

在这个类中,我加载了请求的 XIB,并将按钮链接到一个动作。

问题:

如何使我的按钮在整个表面上响应?

到目前为止我所尝试的:

  • 我只使用了 AutoLayout 来设置按钮的位置。我有 经过测试,约束是正确的。
  • 我尝试在 按钮的 contentEdgeInsets 属性以确保没有 插图。
  • 我试过“theButton.frame = theContainerOfTheButton.frame”

【问题讨论】:

    标签: ios xcode swift uibutton uicollectionview


    【解决方案1】:

    我解决了这个问题!我需要将此添加到我的课程中:typeQuestionVC

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        if(!self.clipsToBounds && !self.hidden && self.alpha>0){
            for subview in self.subviews.reverse(){
                let subPoint = subview.convertPoint(point, toView: self)
                let result = subview.hitTest(subPoint, withEvent: event)
                if result != nil{
                    return result
                }
            }
        }
    
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      相关资源
      最近更新 更多