xcode 为自定义视图不起作用的 UIBarButtonItem 添加目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45035776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:07:21  来源:igfitidea点击:

Add target for UIBarButtonItem with Custom View not Working

iosswiftxcode

提问by auryn31

Hello there,

你好呀,

I have a problem with adding a custom action to a UIBarButtonItem
The Target does not called
Did someone see the problem?

我在向 UIBarButtonItem 添加自定义操作时遇到问题
目标未调用
有人看到问题了吗?

    let navBarMapImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
    navBarMapImageView.contentMode = .scaleAspectFit
    navBarMapImageView.image = image
    navBarMapImageView.isUserInteractionEnabled = true
    navBarMapImageView.target(forAction: #selector(openMaps), withSender: self)
    let navBarMapButton = UIBarButtonItem(customView: navBarMapImageView)

thanks for help

感谢帮助

采纳答案by Bhavesh Dhaduk

You are adding target to UIImageView, it's not work please check below code

您正在将目标添加到UIImageView,它不起作用请检查下面的代码

    let btn1 = UIButton(type: .custom)
    btn1.setImage(UIImage(named: "imagename"), for: .normal)
    btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn1.addTarget(self, action: #selector(openMaps), for: .touchUpInside)
    let item1 = UIBarButtonItem(customView: btn1)

    self.navigationItem.setRightBarButtonItems([item1], animated: true)

回答by Objective D

why don't you just use a UIButton and assign it as bar button item as suggested in this thread:

为什么不直接使用 UIButton 并将其分配为该线程中建议的条形按钮项:

UIBarButtonItem: target-action not working?

UIBarButtonItem:目标操作不起作用?

    let button  = UIButton(type: .custom)
    if let image = UIImage(named:"icon-menu.png") {
        button.setImage(image, for: .normal)
    }

    button.frame = CGRect(x:0.0, y:0.0, width: 30.0, height: 30.0)
    button.addTarget(self, action: #selector(MyClass.myMethod), for: .touchUpInside)
    let barButton = UIBarButtonItem(customView: button)
    navigationItem.leftBarButtonItem = barButton

回答by FBente

Set the target to navBarMapButtonand not to the navBarMapImageView.

将目标设置为navBarMapButton而不是navBarMapImageView.

回答by G?ktu? Aral

I want to add a different approach.

我想添加一种不同的方法。

You can create some custom bar item class and write your specific initializer for customView. For example;

您可以创建一些自定义栏项类并为 customView 编写特定的初始值设定项。例如;

fileprivate class BarButtonItem: UIBarButtonItem {

    @objc func buttonAction()

    override init() {
        super.init()
    }

    convenience init(customView: UIControl) {
        self.init()
        self.customView = customView
        customView.addTarget(self, action: .onBarButtonAction, for: .touchUpInside)
    }

fileprivate extension Selector {
    static let onBarButtonAction = #selector(BarButtonItem.buttonAction)
}

And with this way, you can even add closure inside your buttonAction.

通过这种方式,您甚至可以在 buttonAction 中添加闭包。

var actionCallback: ( () -> Void )?
    @objc func buttonAction() {
        actionCallback?()
    }