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

Add tap action to custom view in NavigationController

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

我试过在视图中添加一个按钮并在那里处理它。我已经尝试让我的导航控制器成为自定义视图的委托,我已经尝试将点击手势识别器添加到导航控制器中的视图。没有任何效果。非常感谢任何建议或反馈。

谢谢!

我的自定义导航控制器:

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()
    }

}

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

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 开始,您需要为自定义视图提供宽度和高度限制,例如:

loadStatus.widthAnchor.constraint(equalToConstant: 44).isActive = true
loadStatus.heightAnchor.constraint(equalToConstant: 44).isActive = true

否则您的自定义视图可能是可见的但在内部已归零。这是一个错误。

(我假设 UINavigationController 现在是 (iOS11+) constraint-based 但之前不是!?)