Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用customView将GestureRecognitor添加到RightBarButtonim_Ios_Swift_Xcode_Uibarbuttonitem - Fatal编程技术网

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

使用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
    }
}
您可以将
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
的初始方法,您可以在此处添加子视图