Search code examples
iosswiftuikituistatusbar

How can I change the frame of the status bar?


I've tried using this to get the frame of the status bar:

var statusBarWindow = UIApplication.sharedApplication().valueForKey("statusBarWindow")

however, when I try to change the frame, it says the value is immutable:

statusBarWindow?.frame = CGRect(x: -oldViewFrame, y: statusBarWindow.frame.origin.y, width: statusBarWindow.frame.size.width, height: statusBarWindow.frame.size.height)

Solution

  • I'm gonna give you an answer based on the comments you've written about your intention with this question.


    Solution

    You can get the statusbar to disappear when opening the sidebar (similar to the Slack app) by overriding the method prefersStatusBarHidden from UIViewController for the sidebar. It should be something like the following:

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    

    You can also modify the appearance of this with the two methods: preferredStatusBarStyle and preferredStatusBarUpdateAnimation


    Example

    I've made a simple project to illustrate this. It is possible to implement a sidebar in many different ways, so instead I've based this example on a popover. Your implementation will depend on how you've implemented the sidebar.

    I've made a simple storyboard with two UIViewController and a UIButton in each. When clicking on the first UIButton, then it will trigger a segue with the type Present As Popover that will show the second controller.

    The first UIViewController has no code in it (everything is done in the storyboard), but the second UIViewController has the code for hiding the status bar.

    I've attached a screenshot of the storyboard and the code for the second UIViewController below.

    //
    //  PopController.swift
    //  SidebarHideStatus
    //
    //  Created by Stefan Veis Pennerup on 31/01/16.
    //  Copyright © 2016 Kumuluzz. All rights reserved.
    //
    
    import UIKit
    
    class PopController: UIViewController {
    
        // MARK: - Storyboard actions
    
        @IBAction func backButtonPressed(sender: UIButton) {
            dismissViewControllerAnimated(true, completion: nil)
        }
    
        // MARK: - Status bar
    
        override func prefersStatusBarHidden() -> Bool {
            return true
        }
    }
    

    Storyboard of the example project, highlighting the segue attached to the <code>UIButton</code>


    UPDATE 1: OP uses a UIView instead of a UIViewController for the sidebar.

    First of all, I would recommend that you factorize your sidebar into a separate UIViewController since it will make it much more reusable in the future, but that's a completely different discussion which could go on for days!

    In order to hide the status bar, then you still need to make use of the callback method I highlighted before, but you simply gotta call the method setNeedsStatusBarAppearanceUpdate in order to manually update it.

    I've updated the initial UIViewController with the following code and deleted the segue in order to demonstrate this approach.

    //
    //  ViewController.swift
    //  SidebarHideStatus
    //
    //  Created by Stefan Veis Pennerup on 31/01/16.
    //  Copyright © 2016 Kumuluzz. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        // MARK: - Properties
    
        private var isSidebarShown = false
    
        // MARK: - Storyboard outlets
    
        @IBAction func buttonPressed(sender: UIButton) {
            isSidebarShown = !isSidebarShown
            setNeedsStatusBarAppearanceUpdate()
        }
    
        // MARK: - Status bar
    
        override func prefersStatusBarHidden() -> Bool {
            return isSidebarShown
        }
    
        override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
            // NOTE: This method has no effect now when
            // using the method setNeedsStatusBarAppearanceUpdate()
            return .Slide
        }
    
    }