限制导航栏隐藏在按钮上而不显示

问题描述 投票:0回答:1

我想在点击时隐藏导航栏。因此,我使用了这种导航栏方法。

 self.navigationController?.hidesBarsOnTap = true

在屏幕上有2个按钮,当我点击该按钮以执行某些操作时,它也隐藏了导航栏。我认为按钮点击被认为是点击。

您能告诉我,这是正确的行为吗?还请让我知道是否有任何限制方法。我不想在单击按钮时隐藏导航栏,屏幕的其余部分都可以。

ios swift button uinavigationbar uitapgesturerecognizer
1个回答
0
投票

您可以创建自定义按钮并处理触摸以启用/禁用隐藏栏,例如:

class BarHideOnTapButton : UIButton {
    weak var navigationController: UINavigationController?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.navigationController?.hidesBarsOnTap = false
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        self.navigationController?.hidesBarsOnTap = true
    }
}

class ViewController: UIViewController {
    @IBOutlet var button: BarHideOnTapButton?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.button?.navigationController = self.navigationController

        self.navigationController?.hidesBarsOnTap = true
    }
...
}
© www.soinside.com 2019 - 2024. All rights reserved.