![](/img/trans.png)
[英]self.navigationItem.rightBarButtonItem?.customView = nil crash
[英]addGestureRecognizer to rightBarButtonItem with customView
如何使用customView
向我的rightBarButtonItem
添加操作?
现在我有代码:
override func viewDidLoad() {
super.viewDidLoad()
configurePhotoProfile()
}
func configurePhotoProfile() {
let imageView = UIImageView()
let widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 34)
let heightConstraint = imageView.heightAnchor.constraint(equalToConstant: 34)
heightConstraint.isActive = true
widthConstraint.isActive = true
imageView.kf.setImage(with: URL(string: channel?.recipientUrlImage ?? ""))
imageView.backgroundColor = .clear
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 17
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(ChatsViewController.didTapImageButton(_:))))
if UserDefaults.standard.integer(forKey: UserDefaultsKeys.selectedSegmentedControl) == 0 {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageView)
} else {
self.navigationItem.rightBarButtonItem = nil
}
}
@objc func didTapImageButton(_ sender: UIBarButtonItem) {
print("tap")
}
但是,如果我点击barButtonItem
,则在控制台中看不到我的print("tap")
。 我的问题在哪里?
像下面这样使用UITapGestureRecognizer
let tap = UITapGestureRecognizer(target: self, action: #selector(ChatsViewController.didTapImageButton(_:)))
tap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tap)
我看到您正在使用基本手势识别器类,您应该使用UITapGestureRecognizer
代替。
当您在视图上以编程方式设置约束时,该视图也是以编程方式创建的,您需要执行以下操作...
imageView.translatesAutoresizingMaskIntoConstraints = false
上述属性需要设为false
才能以编程方式获取工作约束。
另一种选择是您只指定了约束的高度和宽度,但没有指定x position和y position 。 您需要指定(X,Y)位置。
使用图像视图
使用图像视图,您可以执行以下操作。
func configurePhotoProfile() {
let imageView = UIImageView(frame: .init(x: 0, y: 0, width: 34, height: 34))
imageView.kf.setImage(with: URL(string: channel?.recipientUrlImage ?? ""))
imageView.backgroundColor = .clear
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 17
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(ChatsViewController.didTapImageButton(_:)))
tap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tap)
if UserDefaults.standard.integer(forKey: UserDefaultsKeys.selectedSegmentedControl) == 0 {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageView)
} else {
navigationItem.rightBarButtonItem = nil
}
}
您可以将UIButton
添加到栏按钮项(带有自定义视图),而不是图像,如下所示。
let vw = UIButton(frame: .init(x: 0, y: 0, width: 34, height: 34))
vw.addTarget(self, action: #selector(ChatsViewController.didTapImageButton(_:)), for: .touchUpInside)
//set buttons other properties
let barButton = UIBarButtonItem(customView: vw)
navigationItem.rightBarButtonItem = barButton
您正在使用手势识别器的基类,只需在代码中使用UITapGestureRecognizer
更新如下:
override func viewDidLoad() {
super.viewDidLoad()
configurePhotoProfile()
}
func configurePhotoProfile() {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
let widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 34)
let heightConstraint =
imageView.heightAnchor.constraint(equalToConstant: 34)
heightConstraint.isActive = true
widthConstraint.isActive = true
imageView.kf.setImage(with: URL(string: channel?.recipientUrlImage ?? ""))
imageView.backgroundColor = .clear
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 17
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(ChatsViewController.didTapImageButton(_:)))
tap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tap)
if UserDefaults.standard.integer(forKey: UserDefaultsKeys.selectedSegmentedControl) == 0 {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageView)
} else {
self.navigationItem.rightBarButtonItem = nil
}
}
@objc func didTapImageButton(_ sender: UIBarButtonItem) {
print("tap")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.