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.

6 Answers 6

11

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

[viewController.view setFrame: [viewController.view bounds]];
3
  • 4
    [self.view setFrame: [self.view bounds]]; <== I put this instead :)
    – Unreality
    Feb 9, 2009 at 2: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]]; Feb 6, 2010 at 1: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? Mar 26, 2013 at 4: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]];
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.

1
  • 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 6, 2009 at 9: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.

1
  • 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 6, 2009 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.

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.