【问题标题】:Swift UITapGesture on view in a titleView not working在titleView中查看Swift UITapGesture不起作用
【发布时间】:2018-04-02 17:12:07
【问题描述】:

我有一个 UINavigationItem,我将它的 titleView 设置为一个 UIView,它嵌入了一个 UILabel 和 UIImageView。我正在尝试将 UITapGestureRecognizer 添加到视图中,但它似乎不起作用。有什么解决办法吗?此外,向整个导航栏添加手势识别器不是一个选项,因为我有一个 rightBarButtonItem 并且想要使用后退按钮。

这是我的代码:

func configureTitleView() {
    guard let profile = profile else { 
    // Pop navController
    return
  }

    let titleView = UIView()
    titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)

    let containerView = UIView()
    containerView.translatesAutoresizingMaskIntoConstraints = false
    titleView.addSubview(containerView)

    let profileImageView = UIImageView()
    profileImageView.translatesAutoresizingMaskIntoConstraints = false
    profileImageView.contentMode = .scaleAspectFill
    profileImageView.clipsToBounds = true
    let imageURL = URL(string: profile!.firstProfilePicture!)
    profileImageView.sd_setImage(with: imageURL)

    containerView.addSubview(profileImageView)

    profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
    profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 36).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 36).isActive = true

    profileImageView.layer.cornerRadius = 36 / 2

    let nameLabel = UILabel()

    containerView.addSubview(nameLabel)
    nameLabel.text = profile!.displayName!
    nameLabel.textColor = .white
    nameLabel.translatesAutoresizingMaskIntoConstraints = false

    nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
    nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
    nameLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
    nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

    containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
    containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

    self.navigationItem.titleView = titleView

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.openProfile))
    tapGesture.numberOfTapsRequired = 1
    titleView.addGestureRecognizer(tapGesture)
    titleView.isUserInteractionEnabled = true
}

【问题讨论】:

  • 你能分享你的代码,你是如何在导航栏中添加所有这些项目的吗?我希望您在背景视图中添加点击手势,并且图像视图和标签位于这些视图上方,因此手势没有响应。
  • 一个建议,您可以尝试添加一个颜色清晰的按钮,而不是点击手势,就像添加其他两个按钮并为按钮添加目标一样。
  • @Bharath 我已经添加了我的代码...有没有我可以在不使用按钮的情况下采取的方法?
  • 这种情况只发生在 iOS 11 上吗?
  • @beyowulf 我只在 iOS 11 上进行了测试,如果它也是 iOS 10 中的一个错误,我不会感到惊讶。

标签: ios swift uinavigationcontroller uinavigationitem


【解决方案1】:

您不需要为自定义视图添加显式的高度和宽度常量约束。

只需将子视图添加到自定义视图,添加宽度和高度锚点。

    let customView = UIView()

    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    customView.addSubview(button)

    [
        button.widthAnchor.constraint(equalTo: customView.widthAnchor),
        button.heightAnchor.constraint(equalTo: customView.heightAnchor),
        ].forEach({$0.isActive = true})

    navigationItem.titleView = customView

【讨论】:

    【解决方案2】:

    从 iOS 11 开始,使用 UIBarButtonItem(customView:) 添加到工具栏的视图作为 UIBarButtonItem 现在使用自动布局进行布局。这包括通过UIViewControllernavigationItem.titleView 属性添加到UINavigationBar 的标题视图。您应该在 titleView 上添加大小限制。例如:

    titleView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    titleView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
    

    否则,自动布局将使用标题视图的固有内容大小,即CGSize.zero。即使该视图的子视图不是,手势也被隐藏在它们所附加的视图的边界内。因为没有约束的titleView 的边界是CGRect.zero,所以它永远不会触发。添加约束,它按预期工作。

    有关更多信息,请参阅 WWDC 2017 会议Updating your app for iOS 11

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多