我想隐藏导航和状态栏以使其透明。我下面有一张图片,我想占用整个空间。到目前为止,我必须隐藏导航栏,但状态栏仍为白色-这在viewDidLoad()中:

self.navigationController?.setNavigationBarHidden(true, animated: true)

另外,如何将后退按钮重新添加到导航栏中?

谢谢!

我当前的iPhone模拟器的屏幕截图:

ios - 如何在Swift中使iPhone状态栏透明?-LMLPHP

ios - 如何在Swift中使iPhone状态栏透明?-LMLPHP

根据Mohy Gh的代码进行编辑:

最佳答案

Swift 3:整个应用程序的:

AppDelegatedidFinishLaunchingWithOptions方法中执行此操作,而不要隐藏导航栏:

// Sets navigationBar's background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(),
                                                for: .default)

// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = true

编辑:可以在View Controller 中隐藏一个特定View Controller 的navigationBar,例如:
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let navigationBar = self.navigationController?.navigationBar

    navigationBar?.setBackgroundImage(UIImage(), for: .default)
    navigationBar?.shadowImage = UIImage()
    navigationBar?.isTranslucent = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    let navigationBar = self.navigationController?.navigationBar

    navigationBar?.shadowImage = nil
    navigationBar?.setBackgroundImage(nil, for: .default)
    navigationBar?.isTranslucent = false
}

使navigationBar透明后,您需要将imageView的顶部约束设置为topLayoutGuid = 0
如果您还想隐藏一个statusBarviewController,也可以将其放在所需的viewController中:
override var prefersStatusBarHidden: Bool {
    return true
}

关于ios - 如何在Swift中使iPhone状态栏透明?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45395581/

10-12 14:39
查看更多