Amelia Amelia - 6 months ago 20
iOS Question

Status Bar White Opaque on Launch

My initial view controller is a navigation controller and its root view controller is a

UIViewController
which conforms to the
UIPageViewControllerDataSource
protocol. The content for my pages are three unique view controllers which have both a scene in the storyboard and a swift file.

I am hiding the navigation bar in the root view controller's
viewDidLoad()
.

self.navigationController?.navigationBar.hidden = true


When the app launches, the status bar is white and opaque. As soon as I scroll to the next page, it becomes translucent and the background color of the page shows through.

Appearance on Launch

enter image description here

Appearance While Swiping to Next Page

enter image description here

Can someone please help me understand what is happening here and how to fix it? In case it is not obvious, I want the second behavior from launch; the status bar is translucent and the page background shows through.

I have tried


  • In
    viewDidLoad()
    :
    UIApplication.sharedApplication().statusBarStyle = .LightContent

  • In the root view controller:

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


    Followed by
    self.setNeedsStatusBarAppearanceUpdate()
    in
    viewDidLoad()

  • In the root view controller:

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)
    self.navigationController?.navigationBar.barStyle = .Default
    self.navigationController?.navigationBar.barTintColor = UIColor.clearColor()
    }

  • In
    viewDidLoad()
    (above
    navigationBar.hidden
    ):

    self.navigationController?.navigationBar.barStyle = .Default
    self.navigationController?.navigationBar.barTintColor = UIColor.clearColor()



As a note, when I remove the navigation controller and just make the root view controller the initial view controller, the status bar appears as expected- translucent.

This question is similar, but none of the solutions worked, it is over a year old, and when I contacted the poster, he said that he thought he put a view underneath where the status bar would be. I'm not sure that I can manage the the view in such a way that it works seamlessly with the scroll aspect of the page view controller.

Answer

I found a solution. A friend of mine made a suggestion which led me to start thinking about the frame of the UIPageViewController. This led me to grab the height of the status bar and then adjust the frame down by that much.

In 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

I still do not understand why this needs to be manually adjusted. (Hopefully someone else will come along with a solution/explanation.) But I hope this bit of code helps someone.

Comments