0

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)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jay
  • 2,591
  • 1
  • 16
  • 28
  • I need to change the frame of the status bar and [this](https://github.com/ndesai/InstagramStatusBarTest/blob/master/InstagramStatusBarTest/ViewController.m) is the only way I've found online, and it gives me an immutable object. Where can I access the mutable status bar frame? – Jay Jan 31 '16 at 03:16
  • The frame rectangle defining the area of the status bar. (read-only) https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instp/UIApplication/statusBarFrame – Leo Dabus Jan 31 '16 at 03:16
  • @LeoDabus so that implies that status bar frames can't be manipulated in swift, but they can in Obj-c? – Jay Jan 31 '16 at 03:17
  • https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instp/UIApplication/statusBarFrame OBJ-C read-only also – Leo Dabus Jan 31 '16 at 03:19
  • in the slack iOS app, they change the y position of the status bar. Do you know how they did it? – Jay Jan 31 '16 at 03:23
  • They did not change its size they are only using the same tint color for the navigation bar – Leo Dabus Jan 31 '16 at 03:25
  • that's not correct at all, there is an animation shifting it up and down depending on if the side bar is open or not. So can you please help me find a solution on how to accomplish it? – Jay Jan 31 '16 at 03:27

1 Answers1

2

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
    }

}
Kumuluzz
  • 2,012
  • 1
  • 13
  • 17
  • thank you but, i don't think i can implement this within my project because I used a UIView to hold the side bar. I used UIViewAnimation to move the view tied to the current VC and just shifted the sidebar over by the amount I set the original view. So in essence, i'm not popping over any other VC's. So if with one VC, how can I refresh the view in order to re-trigger the prefersstatusbarhidden function? – Jay Feb 02 '16 at 01:55
  • Thank you @Kumuluzz! This solved the problem. I have a question for you though: I subclassed UIView in order to create the sidebar, so it's still reusable, not because it's the best, but it was the only solution I could come up with. What advantages would I have by creating a UIVC over what I did? I definitely want to become a better programmer, so if you can articulate it with as much detail as possible, I would be very grateful! – Jay Feb 02 '16 at 21:32