简体   繁体   English

在 iOS swift 中隐藏导航栏的方式的黑白差异

[英]Differance b/w the way of hiding nav bar in iOS swift

Recently i had one issue.最近我有一个问题。 When in my navigation controller stack (One child controller):在我的导航 controller 堆栈(一个子控制器)中时:

MainScreen -> ScreenA -> ScreenB -> Screenc

So when in my Screenc i want to hide nav bar and status bar.Its was working fine.因此,当在我的Screenc中时,我想隐藏导航栏和状态栏。它工作正常。 But when i come back to my ScreenB my status bar and nav bar are overlap.Not sure why its happend.I search in some google and some SO answers.但是当我回到我的ScreenB时,我的状态栏和导航栏是重叠的。不确定为什么会发生。我在一些谷歌和一些 SO 答案中搜索。 Then i got that isNavigationBarHidden base is UIViewController while isHidden base is UIView .然后我得到isNavigationBarHidden基地是UIViewControllerisHidden基地是UIView

Why i hide my both nav bar and status bar with below code:为什么我用下面的代码隐藏我的导航栏和状态栏:

 override var prefersStatusBarHidden: Bool {
        return true
    }
 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
       self.navigationController?.navigationBar.isHidden = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
       self.navigationController?.navigationBar.isHidden = false
    }

In my prev screen i got the issues of my status bar and nav bar are got overlapeed.在我的上一个屏幕中,我遇到了状态栏和导航栏重叠的问题。

But when i use this code:但是当我使用这段代码时:

 override var prefersStatusBarHidden: Bool {
            return true
        }
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.isNavigationBarHidden = false
    }

Its work well.它运作良好。 There is not overlap in my prev screen.我的上一个屏幕没有重叠。 Why its happening.为什么会发生。 Is there any specific reason behid:背后是否有任何具体原因:

navigationController?.isNavigationBarHidden ( vs ) self.navigationController?.navigationBar.isHidden navigationController?.isNavigationBarHidden ( vs ) self.navigationController?.navigationBar.isHidden

I am very to the iOS Development.我对 iOS 开发非常感兴趣。 Just want to understand the difference.只是想了解其中的区别。 So that i can learn if i am doing any wrong.这样我就可以了解我是否做错了。

Thanks谢谢

Yes, there is a huge difference.是的,有很大的不同。 Very good point!很好的一点!

  • The first one rudely and illegally reaches into the nav controller's interface and manipulates it directly.第一个粗鲁且非法地进入导航控制器的界面并直接操纵它。 You should never interfere with any other view controller's interface directly: not Cocoa's, not your own.你不应该直接干涉任何其他视图控制器的界面:不是 Cocoa 的,也不是你自己的。 Only a view controller should control its own interface.只有一个视图 controller 应该控制自己的接口。

  • The second one politely and correctly instructs the nav controller how to behave.第二个礼貌而正确地指示导航 controller 如何表现。 It is the "public API" for showing and hiding the navigation bar.它是用于显示和隐藏导航栏的“公共 API”。 — Actually the correct approach is to call setNavigationBarHidden(_:animated:) , but setting isNavigationBarHidden calls that for you. — 实际上,正确的方法是调用setNavigationBarHidden(_:animated:) ,但设置isNavigationBarHidden会为您调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM