In iOS 6, hiding the status bar is very simple.
IOS 6 and previously, hide the status bar [[UIApplication sharedapplication] setstatusbarhidden:yes];
After coming to the age of iOS 7, you need to specify in Uiviewcontroller:
#ifdef __iphone_7_0-(BOOL) Prefersstatusbarhidden { return YES;} #endif
and refresh the status bar with the following code:
if ([Viewcontroller respondstoselector: @selector (setneedsstatusbarappearanceupdate)]) { [Viewcontroller Prefersstatusbarhidden]; [Viewcontroller performselector: @selector (Setneedsstatusbarappearanceupdate)];}
But the code above is not all-in-one, and some situations in IOS 7 can cause problems that cannot hide the status bar.
Add a Childviewcontroller in Parentviewcontroller if Parentviewcontroller's
The Prefersstatusbarhidden method returns no, so even if the Prefersstatusbarhidden method in Childviewcontroller returns Yes and calls the code above, the status Bar cannot be hidden.
Solution:Method swizzling
Hook Parentviewcontroller's Prefersstatusbarhidden method in Childviewcontroller, make it return yes, and then invoke the code that updates the status bar to implement the hidden status bar. It is important to note that the hook method needs to be restored in appropriate situations, such as the Viewwilldisappear method of the Childviewcontroller. Doing so may cause strange conditions to arise.
The code is as follows:
1. Implementation of the Prefersstatusbarhidden method of replacing Parentviewcontroller in the Viewdidload method of Childviewcontroller
-(void) viewdidload { [super viewdidload]; _statusbarhidden = [UIApplication sharedapplication].statusbarhidden; Hide status bar when entering interface uiviewcontroller *parentviewcontroller = Self.parentviewcontroller; if ([Parentviewcontroller respondstoselector: @selector (setneedsstatusbarappearanceupdate)]) { [self Hookprefersstatusbarhidden:parentviewcontroller]; } else { //IOS 6 and previously, hide the status bar [[UIApplication sharedapplication] setstatusbarhidden:yes]; }}
2. Implementation of Prefersstatusbarhidden method for replacing Childviewcontroller and Parentviewcontroller with runtime method swizzling Dafa
-(void) Hookprefersstatusbarhidden: (Uiviewcontroller *) Parentviewcontroller { /** Method swizzling 1. If Parentviewcontroller's Prefersstatusbarhidden returns no, then add Childviewcontroller prefersstatusbarhidden on it, even if it returns Yes, You cannot hide the status bar. Therefore, in viewdidload, you need to replace the implementation of Prefersstatusbarhidden method in Parentviewcontroller with 2. At Viewwilldisappear, You need to restore the method of the interchange back to the *. Src_method = Class_getinstancemethod ([Uiviewcontroller class], @selector ( Prefersstatusbarhidden)); Method Des_method = Class_getinstancemethod ([self class], @selector (Hook_prefersstatusbarhidden)); Method_exchangeimplementations (Src_method, des_method); Refresh status bar Dispatch_async (Dispatch_get_main_queue (), ^{ [Parentviewcontroller Prefersstatusbarhidden]; [Parentviewcontroller performselector: @selector (setneedsstatusbarappearanceupdate)]; });} -(BOOL) Hook_prefersstatusbarhidden { //Hide status bar return YES;}
3. When Childviewcontroller is removed from the Parentviewcontroller, the Viewwilldisappear method must be called (be careful not to call it in the Viewdiddisappear method, Childviewcontroller may have been released at this time, so the implementation of the Prefersstatusbarhidden of both can be restored in this method
-(void) Viewwilldisappear: (BOOL) animated { [Super viewwilldisappear:animated]; When exiting the interface, restore the initial state of the status bar uiviewcontroller *parentviewcontroller = Self.parentviewcontroller; if ([Parentviewcontroller respondstoselector: @selector (setneedsstatusbarappearanceupdate)]) { [self Hookprefersstatusbarhidden:parentviewcontroller]; } else { //IOS 6 and previously, restore the initial state of the status bar [[UIApplication sharedapplication] setstatusbarhidden:_statusbarhidden]; }}
Sometimes in order to ensure that the status bar is hidden, you can force the above code to execute.
In the actual project for the second time the characteristics of the runtime, really happy, haha.
Resources:
IOS7 Hide Status bar (battery bar)
Objective-c Hook Scheme (i): Method swizzling
IOS uses method swizzling to hide the status Bar