Swift 在 titleView 中显示的 UITapGesture 不工作

Swift UITapGesture on view in a titleView not working

我有一个 UINavigationItem,我将它的 titleView 设置为一个嵌入了 UILabel 和 UIImageView 的 UIView。我正在尝试将 UITapGestureRecognizer 添加到视图中,但它似乎不起作用。任何解决方案?此外,将 gestureRecognizer 添加到整个 navigationBar 不是一个选项,因为我有一个 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
}

从 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 session Updating your app for iOS 11

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

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

    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({[=10=].isActive = true})

    navigationItem.titleView = customView