ios - UITapRecognizer 不能以编程方式在 UIView 上工作?
问题描述
我无法理解为什么当我的 UIView 被点击时,我没有调用我的函数。我的 UIView 在我的 setUpNavBar() 我做错了什么?我的 viewDidLoad 是一个 tableView,但我在 setUpNavbar 中有一个子视图,其中包含我的标题和图像。我首先通过添加titleView.isUserInterationEnabled = true
将成为解决方案,但事实并非如此。
import UIKit
import Firebase
class MessageController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
var ref: DatabaseReference!
ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users/profile").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot)
if let dictionary = snapshot.value as? [String: AnyObject] {
// self.navigationItem.title = dictionary["username"] as? String
let user = User()
user.setValuesForKeys(dictionary)
self.setUpNavBar(user: user)
}
}, withCancel: nil)
}
func setUpNavBar(user: User) {
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.red
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
if let profileImageUrl = user.photoURL {
profileImageView.loadImageUsingCacheWithURLString(urlString: profileImageUrl)
}
containerView.addSubview(profileImageView)
profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
let nameLabel = UILabel()
containerView.addSubview(nameLabel)
nameLabel.text = user.username
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
titleView.isUserInteractionEnabled = true
let mytapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showChatController))
mytapGestureRecognizer.numberOfTapsRequired = 1
titleView.addGestureRecognizer(mytapGestureRecognizer)
}
@objc func showChatController() {
print("clicked")
// let chatLogController = ChatLogController()
// navigationController?.pushViewController(chatLogController, animated: true)
}
解决方案
你profileImageView
正在“吃掉”这些触摸。尝试禁用它的 userInteraction。它应该允许将触摸传递给它的父级,containerView
profileImageView.isUserInteractionEnabled = false
推荐阅读
- python - 如何使用 python 从 JSON 对象中提取特定数据?
- yaml - 将 Swagger 规范拆分为较小的文件时出错
- api - 图形 api 获取 Teams 配置返回 null
- python - 在 Python 中处理 csv 表,无需 pandas、csv 或任何其他模块或库
- php - 从 PCM 生成 .WAV (AWS Polly)
- excel - 如何动态引用其他工作簿中的另一个单元格?
- javascript - 通过 Jquery 在动态 HTML 表的单元格中执行计算
- java - JSpinner - 奇数步长
- c++ - Clang (MacOS 32/64):在某些函数中禁用堆栈帧
- swagger - 从 Lumen REST API 生成 Swagger API 文档