使用 customView 将 GestureRecognizer 添加到 rightBarButtonItem

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)

我看到您正在使用基本手势识别器 class,您应该改用 UITapGestureRecognizer

  • 当您以编程方式在同样以编程方式创建的视图上设置约束时,您需要执行以下操作...

    imageView.translatesAutoresizingMaskIntoConstraints = false
    
  • 以上属性需要false以编程方式获取工作约束。

  • 另一种选择是您只指定了约束的高度和宽度,但 没有指定 x 位置 y位置.需要指定(X,Y)位置.

带ImageView

使用图像视图,您可以执行以下操作。

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

您正在使用基础 class 作为手势识别器,只需在您的代码中使用 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")
        }

我对您使用 Gesture 获取事件的方法表示担忧。最好使用 IBAction 获取事件。

为此,您可以在 NavigationItem 中添加自定义视图 (UIView),并将 UIImageUIButton 添加为该自定义视图的子视图。

完成所需的 UI 部分后,您可以为添加的按钮创建 IBAction

下图是您想要的初始方法UI您可以在此处添加子视图