标题视图中的Swift UITapGesture无法正常工作

Fua*_*oro 0 uinavigationcontroller uinavigationitem ios swift

我有一个UINavigationItem,我将它的titleView设置为UIView,它嵌入了UILabel和UIImageView.我正在尝试向视图添加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
}
Run Code Online (Sandbox Code Playgroud)

bey*_*ulf 5

与iOS 11开始,观点添加到工具栏的UIBarButtonItem使用UIBarButtonItem(customView:)正在布局使用自动布局.这包括UINavigationBar通过navigationItem.titleViewa属性添加到a的标题视图UIViewController.您应该在您的上添加大小限制titleView.例如:

titleView.widthAnchor.constraint(equalToConstant: 100).isActive = true
titleView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
Run Code Online (Sandbox Code Playgroud)

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

有关更多信息,请参阅WWDC 2017会话更新iOS 11应用程序.