How to set the top position = 0 after setStatusBarHidden:Yes?

后端 未结 6 391
栀梦
栀梦 2020-12-09 11:26

I found that after setting the

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

at viewDidLoad, then if

相关标签:
6条回答
  • 2020-12-09 12:07

    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]];
    
    0 讨论(0)
  • 2020-12-09 12:08

    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:

    • -prefersStatusBarHidden and -setNeedsStatusBarAppearanceUpdate are for iOS 7.0+.
    • -applicationFrame returns the frame for the app window.
    • To test portrait/landscape orientation on iOS simulator, use (rotate left) and (rotate right).
    • To test in-call status bar on iOS simulator, use Y.
    0 讨论(0)
  • 2020-12-09 12:12

    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.

    0 讨论(0)
  • 2020-12-09 12:15

    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.

    0 讨论(0)
  • 2020-12-09 12:18

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

    [viewController.view setFrame: [viewController.view bounds]];
    
    0 讨论(0)
  • 2020-12-09 12:25

    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 讨论(0)
提交回复
热议问题