【问题标题】:Add tap action to custom view in NavigationController在 NavigationController 中将点击操作添加到自定义视图
【发布时间】:2018-04-25 02:03:53
【问题描述】:

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

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

谢谢!

我的自定义导航控制器:

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

}

【问题讨论】:

  • 你想在你的所有 viewControllers 中使用这个自定义视图吗?
  • 不,我只想要当前的。

标签: ios swift uiview uinavigationcontroller uitapgesturerecognizer


【解决方案1】:

从 iOS11 开始,您需要为自定义视图提供宽度和高度约束,例如:

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

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

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

【讨论】:

  • 我忘记结束这个问题了,但问题就是这样!
【解决方案2】:

UIBarButtonItem 添加轻击手势识别器似乎有点奇怪,默认情况下,当创建一个新的UIBarButtonItem 时,如代码 sn-p- 所示,使用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

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

【讨论】:

  • 您好,感谢您的回复。我不知道你可以这样设置自定义视图。我试了一下,它仍然没有使动作火起来。我尝试为自定义视图启用用户交互,但没有任何变化。
  • @EliByers 所以视图已更改但仍无法访问?
猜你喜欢
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多