我的初始 View Controller 是导航 Controller ,其 Root View Controller 是符合 UIPageViewControllerDataSource
协议(protocol)的 UIViewController
。我的页面的内容是三个独特的 View Controller ,它们在 Storyboard 中都有一个场景和一个 swift 文件。
我将导航栏隐藏在 Root View Controller 的 viewDidLoad()
中。
self.navigationController?.navigationBar.hidden = true
当应用程序启动时,状态栏是白色且不透明的。一旦我滚动到下一页,它就会变成半透明,并且页面的背景颜色会显示出来。
启动时的外观
滑动到下一页时的外观
有人可以帮助我了解这里发生了什么以及如何解决它吗?如果它不明显,我想要启动时的第二个行为;状态栏是半透明的,页面背景可以透过。
我试过了
- 在
viewDidLoad()
中:UIApplication.sharedApplication().statusBarStyle = .LightContent
在 Root View Controller 中:
override func preferredStatusBarStyle() -> UIStatusBarStyle { return .Default }
在
viewDidLoad()
中紧跟在 Root View Controller 中:
override func viewWillAppear(animated: Bool) { super.viewWillAppear(true) self.navigationController?.navigationBar.barStyle = .Default self.navigationController?.navigationBar.barTintColor = UIColor.clearColor() }
在
viewDidLoad()
中(在navigationBar.hidden
之上):self.navigationController?.navigationBar.barStyle = .Default self.navigationController?.navigationBar.barTintColor = UIColor.clearColor()
self.setNeedsStatusBarAppearanceUpdate()
请注意,当我删除导航 Controller 并将 Root View Controller 设置为初始 View Controller 时,状态栏会按预期显示 - 半透明。
这question是相似的,但没有一个解决方案有效,它已经超过一年了,当我联系发帖人时,他说他认为他认为他在状态栏所在的下方放置了一个 View 。我不确定我能否以与页面 View Controller 的滚动方面无缝协作的方式管理 View 。
最佳答案
我找到了解决方案。我的一个 friend 提出的建议让我开始思考 UIPageViewController
的框架。这导致我捕获状态栏的高度,然后将框架向下调整那么多。
在viewDidLoad
中:
let pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
// More pageViewController setup
let statusBarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height
let x = pageViewController.view.bounds.origin.x
let y = pageViewController.view.bounds.origin.y - statusBarHeight
let width = pageViewController.view.bounds.width
let height = pageViewController.view.bounds.height + statusBarHeight
let frame = CGRect(x: x, y: y, width: width, height: height)
pageViewController.view.frame = frame
我还是不明白为什么要手动调整这个。 (希望其他人会提出解决方案/解释。)但我希望这段代码对某人有所帮助。
关于ios - 启动时状态栏为白色不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37173254/