Hide status bar in iOS without removing its view

时光毁灭记忆、已成空白 提交于 2021-02-15 06:00:47

问题


I want to hide status bar in my iPhone app on a button press and I want to show it again on pressing another button.

I tried to hide the status bar by overriding -(BOOL)prefersStatusBarHidden in my view controller but this also removes its view from the top.

So a jump is seen on removing this status bar. What I want to do is just to hide the content on the status bar while keeping the background of status bar.
For example: You can check the same functionality in gmail app. When you open the side drawer in gmail app, then only the contents on status bar are hidden and there is no jump.


回答1:


I fixed this problem with additionalSafeAreaInsets property at iOS13. No other solution worked for me at iOS13. When hiding status var set additionalSafeAreaInsets top value to 20, when unhiding set it again to 0.

For devices with notch no need to change safe area insets. Do this for only other devices.




回答2:


  • You need to make the view full screen that shows under the status bar. Keep an extra 20px on top of the view to give space for the status bar.
  • Use autoLayout. but do not reference to the Top Layout Guide or Bottom Layout Guide. Instead use SuperView Top/Bottom
  • Add these lines of code in your viewDidLoad method in your controller or in the container controller if you are using any.

    self.edgesForExtendedLayout = UIRectEdgeAll; self.extendedLayoutIncludesOpaqueBars = YES; self.automaticallyAdjustsScrollViewInsets = NO;

The first Line, self.edgesForExtendedLayout = UIRectEdgeAll; includes all edges for layout. The second, self.extendedLayoutIncludesOpaqueBars = YES; includes opaque bars also in the layout. If your status bar is not translucent , this line covers the case. The third Line, self.automaticallyAdjustsScrollViewInsets = NO; specifies not to adjust the scroll inset of the view.




回答3:


Set a bool as your toggle on/off and return it in the 'prefersStatusBarHidden':

- (BOOL)prefersStatusBarHidden {
    return self.yourBool;
}

To change the statusBar state, change the bool's value and call this method inside an animation block:

self.yourBool = NO;
[self setNeedsStatusBarAppearanceUpdate];



回答4:


    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]){
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    else{
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }


来源:https://stackoverflow.com/questions/28192511/hide-status-bar-in-ios-without-removing-its-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!