如何在 Swift 中使 iPhone 状态栏透明?
Posted
技术标签:
【中文标题】如何在 Swift 中使 iPhone 状态栏透明?【英文标题】:How do I make the iPhone status bar transparent in Swift? 【发布时间】:2018-01-05 19:51:11 【问题描述】:我希望隐藏导航和状态栏以使其透明。我在下面有一张图片,我想占据整个空间。到目前为止,我必须隐藏导航栏,但状态栏仍然是白色的 - 这是在 viewDidLoad()
:
self.navigationController?.setNavigationBarHidden(true, animated: true)
另外,如何将后退按钮重新添加到导航栏?
谢谢!
我当前的 iPhone 模拟器的屏幕截图:
根据 Mohy Gh 的代码编辑:
【问题讨论】:
【参考方案1】: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
编辑:为您的视图控制器中的一个特定视图控制器隐藏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
如果你还想为一个viewController
隐藏statusBar
,也可以把它放在你想要的viewController 中:
override var prefersStatusBarHidden: Bool
return true
【讨论】:
如果我只想为一个特定的视图控制器而不是全部隐藏它怎么办? @Sam 我编辑了我的帖子 :) 如果它有效,请选择它作为正确答案 :) 它已经到了,但我实际上不想隐藏状态栏!我只是想要它,以便我的图片占据状态栏和向下的整个空间。这样我仍然可以看到状态栏,它的信息就在图片的上方。所以导航栏消失了,但状态栏是透明的。接近我的第一个屏幕截图 所以只需忽略覆盖prefersStatusBarHidden
,您可以通过使 navigationBar
透明来实现您想要的,并将您的 imageView
的顶部约束设置为 topLayoutGuid
:S
哦,好吧-我想我让它工作了。那我还能在照片上添加后退按钮吗?喜欢导航栏中的原始后退按钮吗?以上是关于如何在 Swift 中使 iPhone 状态栏透明?的主要内容,如果未能解决你的问题,请参考以下文章