【问题标题】:Restrict navigation bar from hide on button tap限制导航栏在按钮点击时隐藏
【发布时间】:2020-04-17 17:32:41
【问题描述】:

我想在点击时隐藏导航栏。所以我就用了这种导航栏的方式。

 self.navigationController?.hidesBarsOnTap = true

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

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

【问题讨论】:

    标签: ios swift button uinavigationbar uitapgesturerecognizer


    【解决方案1】:

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

    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
        }
    ...
    }
    

    【讨论】:

    • 您是否在故事板或 xib 中为按钮设置了 class BarHideOnTapButton
    • 这个解决方案对我有用。尝试在 touchesBegan 和 touchesEnded 中设置断点,以确保在单击按钮时调用它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多