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

时间:2017-07-30 01:39:15

标签: ios swift statusbar

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

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

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

谢谢!

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

enter image description here

Screen shot after add Mohy Gh's code

编辑Mohy Gh的代码:

1 个答案:

答案 0 :(得分:1)

整个应用程序的

Swift 3:

AppDelegate的{​​{1}}方法中执行此操作,而不是隐藏导航栏:

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的{​​{1}},也把它放在你想要的viewController中:

statusBar