Ios 在NavigationController中将点击操作添加到自定义视图

Ios 在NavigationController中将点击操作添加到自定义视图,ios,swift,uiview,uinavigationcontroller,uitapgesturerecognizer,Ios,Swift,Uiview,Uinavigationcontroller,Uitapgesturerecognizer,我正在尝试向导航栏中的自定义视图添加操作。我有它显示良好,但我不知道如何添加一个点击动作到它 我尝试在视图中添加一个按钮并在那里处理它。我已经尝试将我的导航控制器作为自定义视图的代理,我已经尝试在导航控制器的视图中添加轻触手势识别器。什么都没起作用。非常感谢您的任何建议或反馈 谢谢 我的自定义导航控制器: class MainNavVC: UINavigationController { var loadStatus = LoadStatus() override func

我正在尝试向导航栏中的自定义视图添加操作。我有它显示良好,但我不知道如何添加一个点击动作到它

我尝试在视图中添加一个按钮并在那里处理它。我已经尝试将我的导航控制器作为自定义视图的代理,我已经尝试在导航控制器的视图中添加轻触手势识别器。什么都没起作用。非常感谢您的任何建议或反馈

谢谢

我的自定义导航控制器:

class MainNavVC: UINavigationController {

    var loadStatus = LoadStatus()

    override func viewDidLoad() {
        super.viewDidLoad()

        // load status
        loadStatus.bounds = CGRect(x: -24, y: -6, width: 0, height: 0)
        let loadStatusButton = UIBarButtonItem(customView: loadStatus)
        self.viewControllers.last?.navigationItem.leftBarButtonItem = loadStatusButton

        let tap = UITapGestureRecognizer(target: self, action: #selector(loadStatusPressed))
        loadStatus.addGestureRecognizer(tap)
        loadStatus.isUserInteractionEnabled = true
    }

    @objc func loadStatusPressed(recognizer: UIGestureRecognizer) {
        print("tapped")
        let alert = UIAlertController(title: "Load Status", message: "When you are under a load, you are being tracked by a shipper.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
}
我的自定义视图:

class LoadStatus: UIView {

    @IBOutlet var contentView: UIView!

    private func commonInit(){
        Bundle.main.loadNibNamed("LoadStatus", owner: self, options: nil)
        addSubview(contentView)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder ) {
        super.init(coder: aDecoder)
        commonInit()
    }

}

默认情况下,在创建新的
uibarbuttoneim
时,为
uibarbuttoneim
添加点击手势识别器似乎有点奇怪-如代码片段中所示-通过使用,您可以为条形按钮添加所需的操作,例如:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // like this:
        let loadStatusButton = UIBarButtonItem(title: "Button Title", style: .plain, target: self, action: #selector(loadStatusPressed))


        // ...
    }

    @objc func loadStatusPressed() {
        print("tapped")
        let alert = UIAlertController(title: "Load Status", message: "When you are under a load, you are being tracked by a shipper.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
}
为了设置自定义视图,您只需添加:

loadStatusButton.customView = loadStatus

因此,无需为条形按钮添加点击手势。

UIBarButtonItem
添加点击手势识别器似乎有点奇怪,默认情况下,当创建新的
UIBarButtonItem
时-如代码片段中所示-通过使用,您可以为条形按钮添加所需的操作,例如:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // like this:
        let loadStatusButton = UIBarButtonItem(title: "Button Title", style: .plain, target: self, action: #selector(loadStatusPressed))


        // ...
    }

    @objc func loadStatusPressed() {
        print("tapped")
        let alert = UIAlertController(title: "Load Status", message: "When you are under a load, you are being tracked by a shipper.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
}
为了设置自定义视图,您只需添加:

loadStatusButton.customView = loadStatus

因此,无需为条形按钮添加点击手势。

从iOS11开始,您需要为customview提供宽度和高度约束,如:

loadStatus.widthAnchor.constraint(equalToConstant: 44).isActive = true
loadStatus.heightAnchor.constraint(equalToConstant: 44).isActive = true
否则,您的customview可能可见,但内部大小为零。这是一只虫子


(我假设UINavigationController现在(iOS11+)是基于约束的,但不是以前!?)

从iOS11开始,您需要为customview提供宽度和高度约束,如:

loadStatus.widthAnchor.constraint(equalToConstant: 44).isActive = true
loadStatus.heightAnchor.constraint(equalToConstant: 44).isActive = true
否则,您的customview可能可见,但内部大小为零。这是一只虫子


(我假设UINavigationController现在(iOS11+)是基于约束的,但不是以前!?)

您想在所有的ViewController中使用此自定义视图吗?不,我只想在当前的ViewController中使用它。您想在所有ViewController中使用此自定义视图吗?不,我只想在当前的ViewController中使用它。您好,谢谢您的回复。我不知道你可以这样设置自定义视图。我给它打了一针,但它仍然不能让动作发生。我尝试为自定义视图启用用户交互,但没有更改。@EliByers,因此视图已更改,但仍无法访问?您好,感谢您的回复。我不知道你可以这样设置自定义视图。我给它打了一针,但它仍然不能让动作发生。我尝试为自定义视图启用用户交互,但没有更改。@EliByers因此视图已更改,但仍然无法访问?我忘记关闭此问题,但这就是问题所在!我忘了结束这个问题,但这就是问题所在!