我希望隐藏导航和状态栏以使其透明。我在下面有一张图片,我想占据整个空间。到目前为止,我必须隐藏导航栏但状态栏仍然是白色 - 这是在viewDidLoad()
:
self.navigationController?.setNavigationBarHidden(true, animated: true)
另外,如何将后退按钮添加到导航栏?
谢谢!
我目前的iPhone模拟器的屏幕截图:
编辑Mohy Gh的代码:
Swift 3:适用于整个应用:
在你的AppDelegate
的didFinishLaunchingWithOptions
方法中执行此操作,而不是隐藏导航栏:
// 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控制器中的一个特定视图控制器隐藏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
如果你想隐藏statusBar
为一个viewController
也把它放在你想要的viewController:
override var prefersStatusBarHidden: Bool {
return true
}