12

I found that after setting the

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO]

at viewDidLoad, then if I want to align an image to the top, I will need to set its position as .y = -20;

Is there anyway to make the y coordinate of the top position to 0 ? or is it doomed to be -20 after hidding the status bar?

Thanks for reading.

Binarian
  • 12,296
  • 8
  • 53
  • 84
Unreality
  • 4,111
  • 11
  • 48
  • 67

6 Answers6

11

I had a similar problem at one point and this bit of code fixed it for me:

[viewController.view setFrame: [viewController.view bounds]];
Justin Gallagher
  • 3,242
  • 21
  • 25
  • 4
    [self.view setFrame: [self.view bounds]]; <== I put this instead :) – Unreality Feb 09 '09 at 02:16
  • 5
    Using view.bounds (for me at least) correctly moves the view to 0,0, but still leaves a 20px gap at the *bottom* (noticeable because my view's background color is not white). This works: [self.view setFrame: [[UIScreen mainScreen] bounds]]; – Brian Moeskau Feb 06 '10 at 01:39
  • 4
    This answer makes no sense at all. You're clearly going to end up with a 20 point gap at the bottom of your view. Can someone *please* remove this as the correct answer? – Benjamin Dobell Mar 26 '13 at 04:08
4

Justin Gallagher's solution is almost right, but has one major side effect.

Hiding the status bar and then setting the view's frame to its own bounds will work in the current orientation. But rotation will be ugly. If you are in portrait, for instance, rotating the device to landscape will cause the entire view's frame to be shifted to the right 256 points, leaving a large black space on screen.

bmoeskau's solution (to another side effect) in the comments above avoids this problem:

[self.view setFrame: [[UIScreen mainScreen] bounds]];
Steve Cotner
  • 505
  • 4
  • 14
3

I imagine you should add a UIStatusBarHidden item to the Info.plist if you want the status bar to be removed from start.

Discussion@apple.com

You also could look into setting the Autosizing to resize vertical (and horizontal)

See under Add A Text View here for example of what to click on in InterfaceBuilder

Quote:

Click the horizontal and vertical lines in the internal box so that they become solid red lines. The animated preview shows that the text view's internal size will grow and shrink with the window.

epatel
  • 45,805
  • 17
  • 110
  • 144
  • Many thanks for your reply, but it is doing no good to me.. setting it both to YES/true still unable to remove the status bar... and then I found that the code works instead of adding this key. I guess I must have done something wrong – Unreality Feb 06 '09 at 09:58
2

Check to make sure the size of your root view in your nib is properly set to 480x320. Some of the template project create those at 460x320 to account for the status bar. If you load a view that span full screen and the status bar is hidden, it should just work and you shouldn't need to do anything special at all.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • Thanks for your reply, and I have already set it 480x320, but it's leaving a 20 pixels white space at the top afterwards. – Unreality Feb 06 '09 at 10:00
0

If you have a scroll view nested inside a view make sure to change that also. This code solved all my issues.

[self.view setFrame: [self.view bounds]];
[self.theScroller setFrame: [self.view bounds]];

"theScroller" is the name of my scrollview.

mikemike396
  • 2,435
  • 1
  • 26
  • 41
0

The following solution works correctly

  • on both iOS 6 and iOS 7,
  • for both hiding and showing the status bar,
  • with both portrait and landscape orientation, and
  • even with the in-call status bar visible

In your view controller, add these:

- (BOOL)prefersStatusBarHidden  // For iOS 7.0 or above.
{
    return _isStatusBarHidden;
}

// Call this method to show / hide the status bar and resize the view.
- (void)setStatusBarHidden:(BOOL)isStatusBarHidden
{
    _isStatusBarHidden = isStatusBarHidden;

    // For iOS 7.0 or above...
    if([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        // Update status bar visibility.
        [self setNeedsStatusBarAppearanceUpdate];   // Tell the view controller that the return value of -prefersStatusBarHidden has changed.
    }
    // Otherwise...
    else
    {
        // Show or hide status bar.
        [[UIApplication sharedApplication] setStatusBarHidden:_isStatusBarHidden withAnimation:UIStatusBarAnimationNone];

        // Resize view.
        self.view.frame = [UIScreen mainScreen].applicationFrame;
    }
}

Here, BOOL _isStatusBarHidden; is a member variable of the view controller class.

Notes:

Pang
  • 9,564
  • 146
  • 81
  • 122