2

I am adding a transparent look to my statusbar using the following piece of code in my AppDelegate.swift:

   statusBar = UIView(frame: CGRectMake(0, 0, self.window!.frame.width, 20))
   statusBar!.backgroundColor = someTransparentColor // customized UIColor
   self.window!.rootViewController!.view.addSubview(statusBar!)

This is the structure of my app:

      X    Y
      |    |
      NC   NC
      |    |
NC -> A -> B -> C       (NC = Navigation Controller)

The above code works fine for views A, B, C; but does not apply to X and Y (This is because I added the statusBar subview to the rootViewController). What is it that I can do to make it applicable to X and Y as well? Of course I can copy that code in each respective view controller, but I wan't to avoid that!

Note: I have X and Y use Separate Navigation Controllers, as I am presenting them Modally, and the NCs make it easy to add a Navigation Bar/ToolBar, especially because they are UITableViewControllers with static tables.

Kushal
  • 8,100
  • 9
  • 63
  • 82
rgamber
  • 5,749
  • 10
  • 55
  • 99
  • Why not use a UIView subclass, e.g. MyStatusBarView? Now it can configure _itself_, and you just use it where you want. – matt Apr 15 '15 at 04:23
  • Thats an interesting suggestion! The thing is I am also detecting the orientation change in AppDelegate, and removing it from superview it. Is there a way I could do that if I use the subclass? Probably `self.removeFromSuperView` or so. In any case, thanks for the suggestion! I am looking into it! – rgamber Apr 15 '15 at 04:28
  • @matt , this way eliminates some code duplication, though in every view controller or tableviewcontroller that is not connected to the root directly, I have to manually add the view in portrait mode and remove in landscape. The remove part I can handle in the subclass, but the adding the view back in portrait mode still needs to be handled in each viewController. So still a lot of same code being repeated..! – rgamber Apr 15 '15 at 05:04
  • Then why don't you subclass UINavigationController and use that subclass throughout, so that everywhere you need a navigation controller, your subclass can take care of adding this extra view? – matt Apr 15 '15 at 05:06
  • Indeed, but several of those are UITableViewControllers and some are UICollectionViewControllers. So I'll have to superclass each of it. Probably not worth the effort! I wonder why there is no simple way to change the color of the status bar directly! – rgamber Apr 15 '15 at 05:08
  • Or, why don't you create your navigation controllers using the `initWithNavigationBarClass:toolbarClass:` initializer? This allows you to specify the class of your toolbar to be your special UIToolbar subclass... – matt Apr 15 '15 at 05:08
  • The problem is that I don't even understand what you mean by "status bar". There is no such built-in thing, so I don't get what you're even trying to do. Can you express it in standard terms? – matt Apr 15 '15 at 05:09
  • For example, if you mean UIToolbar, there _is_ a way to make all toolbars have the same color throughout your app. – matt Apr 15 '15 at 05:10
  • Well, by status bar I mean the topmost bar which shows the signal strength etc. The style of it can be changed by using `UIApplication.sharedApplication().statusBarStyle = .LightContent`. Hence I call it a StatusBar. Subclassing the Navigation Controllers is something I was just looking at! Thanks for the awesome suggestions! – rgamber Apr 15 '15 at 05:11
  • I don't understand the problem here. I just made a quick sample project. NC -> view (just has a button that calls a segue) -> NC -> other view (called by button in first view). I used your code to make my status bar blue, and when I ran it, it was blue on both views. – Nick Apr 15 '15 at 05:14
  • Right, I see what you mean. _That_ status bar. :) Well, of course I regard what you're trying to do as violating the spirit of how an iOS 7/8 app is supposed to behave, so I'd better stop trying to help... :)))) – matt Apr 15 '15 at 05:16
  • Haha.. yeah that status bar! Yeah, it kinda does go against the defaults, but I don't see why there is no option to match the status bar color with the navigation bar, when done through storyboard automatically does so for opaque colors! – rgamber Apr 15 '15 at 05:18

1 Answers1

2

Go to your Applications info.plist

  1. Set View controller-based status bar appearance to NO
  2. Set Status bar style to UIStatusBarStyleLightContent

Then go to your Application's AppDelegate and paste the following code where you set your Windows's RootViewController.

#define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

if (IS_OS_6_OR_LATER)
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0)];
    view.backgroundColor = [UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}
Vaibhav Jhaveri
  • 1,579
  • 3
  • 28
  • 53