4
votes

I wanted to toggle the statusBar in my iOS v5 universal app.

But found as it seems others have that UIViews (in my case subViews added to the main view) only reclaim the 20px left by the hidden statusBar if you then rotate the device (I am using the simulator )

And my views are done programmatically.

I tried some of the suggestions found by googling but none seemed to work. i.e changing autoresizing/mask, wantsFullScreenLayout, setNeedsLayout, in IB 'use full screen.'

I finally realised that I was setting my views frame height to 0 and it's bounds height to the [[UIScreen mainScreen] applicationFrame] size.height

What I came up with is: setting the views frame height to -20 and it's bounds height to the [[UIScreen mainScreen] applicationFrame] size.height+20

In the viewDidLoad.

CGRect theDeviceRect= [[UIScreen mainScreen] applicationFrame];
newMap.frame = CGRectMake(0, -20, theDeviceRect.size.width, theDeviceRect.size.height+20);

I also have this in the willAnimateRotationToInterfaceOrientation: delegate.

Now when I toggle the status bar with:

- (void) toggleStatusBarAction:(id)sender{
  BOOL istathidden=  [[UIApplication sharedApplication] isStatusBarHidden];

   [[UIApplication sharedApplication] setStatusBarHidden:!istathidden withAnimation:UIStatusBarAnimationFade];
    }

All works as expected. But I wanted to see if my solution is good or bad way of going about this and if I am missing anything?:

Many thanks in advance. MH

1
I've run into all sorts of problems using negative coordinates like this... how is your view set up? Do you add a root view controller to your window? Or do you add subviews directly to the window? - joerick
I started with the Xcode template. Which adds a root view controller. I then add the views via:[self.view addSubview:newMap]; - markhunte
@joerick . Any reason for your question?? - markhunte
I had a similar problem when I added subviews to a window, rather than using the root view controller. - joerick
I know you already mentioned it, but the autoresizing flags would be my only other idea. Take out all the negative positioning and make sure that the flags fix the margin sizes and allow flexible width and height. - joerick

1 Answers

7
votes

What I do is:

After this code:

[[UIApplication sharedApplication] setStatusBarHidden:NO];

I immediately execute:

CGRect originalFrame = self.view.frame;
originalFrame.origin.y = 20.0;
self.view.frame = originalFrame;

This would kind of "refreshing" the frame to the correct position. Not a best solution, but it works.

In your example, you may need to dynamically set y to 0 or 20 depending on istathidden.

Edit on 2/SEP/2012: A better solution when dismissing the modal view controller:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
self.view.frame = [[UIScreen mainScreen] applicationFrame];