Ios UICapgestureRecognitor未在导航栏标题视图中的UIView上工作?

Ios UICapgestureRecognitor未在导航栏标题视图中的UIView上工作?,ios,swift,uinavigationbar,uitapgesturerecognizer,Ios,Swift,Uinavigationbar,Uitapgesturerecognizer,我试图在导航栏上添加点击手势标题视图,但没有得到任何事件。请告诉我如何解决这个问题 let titleView = UIView() titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 60) titleView.backgroundColor = UIColor.yellow let profileImageView = UIImageView() profileImageView.contentMode = .scal

我试图在
导航栏上添加
点击手势
标题视图
,但没有得到任何事件。请告诉我如何解决这个问题

 let titleView = UIView()
 titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 60)
 titleView.backgroundColor = UIColor.yellow

 let profileImageView = UIImageView()
 profileImageView.contentMode = .scaleAspectFill
 profileImageView.layer.cornerRadius = 20
 profileImageView.clipsToBounds = true
 profileImageView.loadImageUsingCacheWithUrlString(urlString: user.image)
 titleView.addSubview(profileImageView)
 profileImageView.translatesAutoresizingMaskIntoConstraints = false
 profileImageView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
 profileImageView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
 profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
 profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

 let nameLabel = UILabel()
 nameLabel.text = user.name
 nameLabel.font = UIFont(name: "HelveticaNeue-Medium", size: 17)
 titleView.addSubview(nameLabel)
 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: titleView.rightAnchor).isActive = true
 nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

 self.navigationItem.titleView = titleView
 titleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatTableViewController)))

确保将
isUserInteractionEnabled
设置为
true
。默认情况下为true,但如果在您的情况下不起作用,则尝试设置
true

确保调试代码并检查添加标题视图后是否能够打印出
self.navigationItem.titleView

let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 60)
titleView.backgroundColor = UIColor.yellow
titleView.isUserInteractionEnabled = true

self.navigationItem.titleView = titleView
titleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatTableViewController)))

@objc func showChatTableViewController() {
    print("tapped")
}

显示您的showChatTableViewController功能dd
titleView.isUserInteractionEnabled=true