简体   繁体   中英

iOS Hide status bar for iPhone 6 and 6+

i am trying to hide the status bar only for iPhone 6 and 6+ this is what i tried so far.

if (screenWidth == 375) {
        // Remove status bar for iPhone 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES
                                                withAnimation:UIStatusBarAnimationFade];
    }else if (screenWidth == 414){
        // Remove status bar for iPhone 6 +
        [[UIApplication sharedApplication] setStatusBarHidden:YES
                                                withAnimation:UIStatusBarAnimationFade];
    }

You can do (changing plist file):

set Status bar is initially hidden = YES

add row:

View controller-based status bar appearance = NO

Add following line in viewDidLoad:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

and add new method

- (BOOL)prefersStatusBarHidden {
          return YES;
  }

also change info.plist file

View controller-based status bar appearance" = NO

And also add condition for iPhone 6 and 6 Plus.Here are the methods for iPhone 6 and 6 Plus:

/*=====================================================================================================================
 Checks if the device has 4.7 inch screen such as iPhone6 generation
 =====================================================================================================================*/
+(BOOL) ISiPhone6
{
    BOOL isIpad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    // we need to check the maximum of width and height because some screens (the camera view while scanning) we can
    // rotate to portrait or landscape and in the case the screen height and width flip
    return (!isIpad && MAX(screenRect.size.height,screenRect.size.width) == 667);
}

/*=====================================================================================================================
 Checks if the device has 5.5 inch screen such as iPhone6 plus 
 =====================================================================================================================*/
+(BOOL) ISiPhone6Plus
{
    BOOL isIpad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    // we need to check the maximum of width and height because some screens (the camera view while scanning) we can
    // rotate to portrait or landscape and in the case the screen height and width flip
    return (!isIpad && MAX(screenRect.size.height,screenRect.size.width) == 736);
}  

it's works for me.

First: set this flag View controller-based status bar appearance to YES in the info.plist or add it as a new row
Second: override this method - (BOOL) prefersStatusBarHidden in each VC info you want to hide or see the status bar. For child view controller you need also to implement this method - (UIViewController *)childViewControllerForStatusBarHidden
Third: if you are changing status bar appearance at runtime you need to call his method to trigger the animation -setNeedsStatusBarAppearanceUpdate
All this methods help you in create a granular control on status bar appearance.
If you need yo make the status bar disappear while launching just flag Hide Status Bar in your target general settings.

Since you just want to hide Status bar only in iPhone 6 and iPhone 6 Plus then You can do it like below. First add this in your class.

   #import <sys/utsname.h> 

Then in your viewDidLoad method

 NSString *platform;
struct utsname systemInfo;
uname(&systemInfo);
platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

if ( [platform  isEqual:@"iPhone6,1"]||[platform  isEqual:@"iPhone6,2"]){
[[UIApplication sharedApplication] setStatusBarHidden:YES
                                            withAnimation:UIStatusBarAnimationFade];
}

I have posted an answer for a similar question, you have to use the windowLevel of the UIApplication to hide/show the statusBar . Also we have to set the Viewcontroller based appearance property in the info.plist to NO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM