Ios 使用customView将GestureRecognitor添加到RightBarButtonim
如何使用Ios 使用customView将GestureRecognitor添加到RightBarButtonim,ios,swift,xcode,uibarbuttonitem,Ios,Swift,Xcode,Uibarbuttonitem,如何使用customView将操作添加到我的rightBarButtonItem 现在我有了代码: override func viewDidLoad() { super.viewDidLoad() configurePhotoProfile() } func configurePhotoProfile() { let imageView = UIImageView() let widthConstraint = imageView.widthAnchor.co
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
我就看不到控制台中的打印(“点击”)
。我的问题在哪里?使用UITapgestureRecognitizer如下所示
let tap = UITapGestureRecognizer(target: self, action: #selector(ChatsViewController.didTapImageButton(_:)))
tap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tap)
我看到您正在使用基本手势识别器类,您应该使用
uitappesturerecognizer
。- 当您在视图上以编程方式设置约束时(视图也是以编程方式创建的),您需要执行以下操作
imageView.translatesAutoresizingMaskIntoConstraints = false
- 上述属性需要使
以编程方式获取工作约束false
- 另一个选项是,您只指定了约束的高度和宽度,但没有指定x位置和y位置。您需要指定(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
}
}
您可以将ui按钮
添加到条形按钮项(具有自定义视图),而不是图像,如下所示
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
您正在为手势识别器使用基类,只需在代码中使用
uitappesturerecognizer
,更新如下:
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")
}
我对您使用
手势获取事件的方法感到担忧。最好使用iAction
获取事件
为此,您可以在导航项中添加自定义视图(UIView
),并将UIImage
和UIButton
添加为该自定义视图的子视图
完成所需的UI
部分后,可以为添加的按钮创建iAction
下图是所需UI
的初始方法,您可以在此处添加子视图