【问题标题】:iOS - How can I click a UIButton behind UITableView with transparent headeriOS - 如何单击带有透明标题的 UITableView 后面的 UIButton
【发布时间】:2016-11-04 13:29:34
【问题描述】:

我在 UIViewController 的底层有一个 UIButton。 我在 UIButton 顶部有一个 UITableView(全屏尺寸),UITableView 有一个标题(UIView),它有一个透明背景,可以看穿并显示 UIButton。

即使按钮出现在 tableview 标题后面,UIButton 也是不可点击的。

我的tableView的单元格和tableView的表头有按钮,所以无法设置headerView.userInteraction = truetableView.userInteraction = true

我尝试使用pointInside:withEvent:hitTest:withEvent:,UIButton在这两种情况下仍然无法点击。

有什么建议吗?谢谢

【问题讨论】:

  • 为什么不在标题中的uiview中添加按钮?
  • 有什么原因不能只将按钮添加到 tableview 的标题中?
  • 我在 tableView 后面有一个背景图像,当 tableView 滚动时它不会滚动。该按钮将与该背景保持一致。这就是为什么我将按钮放在 tableView 后面而不是放在标题中的原因。

标签: ios uitableview uibutton clickable


【解决方案1】:

你可以像这样使用UITapGestureRecognizer

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    ... 
    let headerViewGesture = UITapGestureRecognizer(target:self, action:#selector(MyClass.headerViewTap(_:)))
    headerView.addGestureRecognizer(mainViewGesture)
    ...
}

func headerViewTap(recognizer: UITapGestureRecognizer) {
    if recognizer.state == UIGestureRecognizerState.Recognized
    { 
       let touchLocation = recognizer.locationInView(recognizer.view)
       print(touchLocation)
       if CGRectContainsPoint(myButton.frame, touchLocation) {
            print("button was touched")
       }
    }
}

【讨论】:

    【解决方案2】:

    请尝试 bringSubviewToFront 以使按钮可点击。

           self.view.bringSubviewToFront(yourButtton)
    

    或者您可以在故事板中检查按钮是否在某个视图内,并添加确保按钮正确放置在故事板中。该按钮应位于表格视图下方,如示例图片中所示。

    这两种方法你都可以尝试,如果有任何疑问,请告诉我。

    【讨论】:

      猜你喜欢
      • 2014-06-24
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2018-04-29
      相关资源
      最近更新 更多