UITapGestureRecognizer无法在navigationBar titleView中的UIView上使用?

时间:2019-05-25 06:05:13

标签: ios swift uinavigationbar uitapgesturerecognizer

我正在尝试在tapGesture navigationBar上添加titleView,但是没有任何事件。请告诉我如何解决此问题。

 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)))

1 个答案:

答案 0 :(得分:1)

确保将const psWithNonEmptyClass = document.querySelectorAll("p[class]:not([class=''])"); console.log(psWithNonEmptyClass.length);设置为<p class="">Hello, world!</p> <p class="5">Hello, world!</p> <p class="9">Hello, world!</p> <p class="">Hello, world!</p>。默认情况下为true,但是如果您的情况无法使用,请尝试设置isUserInteractionEnabled

确保调试代码,并检查添加titleView后是否可以打印出true

true