ios - 启动时状态栏为白色不透明

标签 ios uinavigationcontroller uipageviewcontroller uistatusbar

我的初始 View Controller 是导航 Controller ,其 Root View Controller 是符合 UIPageViewControllerDataSource 协议(protocol)的 UIViewController 。我的页面的内容是三个独特的 View Controller ,它们在 Storyboard 中都有一个场景和一个 swift 文件。

我将导航栏隐藏在 Root View Controller 的 viewDidLoad() 中。

    self.navigationController?.navigationBar.hidden = true

当应用程序启动时,状态栏是白色且不透明的。一旦我滚动到下一页,它就会变成半透明,并且页面的背景颜色会显示出来。

启动时的外观

enter image description here

滑动到下一页时的外观

enter image description here

有人可以帮助我了解这里发生了什么以及如何解决它吗?如果它不明显,我想要启动时的第二个行为;状态栏是半透明的,页面背景可以透过。

我试过了

  • viewDidLoad()中:UIApplication.sharedApplication().statusBarStyle = .LightContent
  • 在 Root View Controller 中:

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .Default
    }
    

    viewDidLoad()

  • 中紧跟self.setNeedsStatusBarAppearanceUpdate()
  • 在 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()
    

请注意,当我删除导航 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/

相关文章:

ios - NSFetchedResultsController 和实体继承

ios - 删除 uinavigationbar 标题边距

ios - 具有多个 View 和弹跳的 UIPageViewController

IOS:didSelectRowAtIndexPath 在选择单个单元格时选择多个单元格

ios - 如何在 Swift 中将 Int 转换为字符

ios - navigationBar出现在statusBar下面

ios - 如何向导航项添加按钮?

ios - 向 uipageviewcontroller 添加分页

Swift 3 - UIPageViewController - 不鼓励在分离的 View Controller 上呈现 View Controller

ios - 如何在 Swift 中创建自动完成文本字段