Deep understanding of IOS status bar _ios

Source: Internet
Author: User
Tags comments

One, the status bar of the hidden

There are two main ways to hide the status bar, let's look at it together.

Method one: Through code control

@interface uiapplication (uiapplicationdeprecated)

//Setting Statusbarhidden does nothing if your application is Using the default uiviewcontroller-based status bar system.
@property (ReadWrite, Nonatomic,getter=isstatusbarhidden) BOOL Statusbarhidden 
Ns_deprecated_ios (2_0, 9_0, "use- [Uiviewcontroller Prefersstatusbarhidden] ") __tvos_prohibited;

-(void) Setstatusbarhidden: (BOOL) hidden animated: (bool) animated 
Ns_deprecated_ios (2_0, 3_2) __tvos_prohibited;
 Use-setstatusbarhidden:withanimation:
-(void) Setstatusbarhidden: (BOOL) Hidden withanimation: ( uistatusbaranimation) Animation 
Ns_deprecated_ios (3_2, 9_0, "use-[uiviewcontroller Prefersstatusbarhidden]") _ _tvos_prohibited;

Note: Let's take a look at // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system. this comment tip, Apple prompts the developer to use a system based status bar style your settings are not effective, in the next to be introduced through the Info.plist hidden status bar to also pay attention to this matter.

Info.plistAdd a View controller-based status bar appearance setting option in to, set to no so you can use the method above.

Note: The View controller-based status bar appearance type is added Bool , the default is yes, unfortunately iOS9 Apple has not recommended these methods, these methods can be used but will report a warning.

So how do you do that when these methods are disabled? The comments have suggested Use -[UIViewController prefersStatusBarHidden] that this is the UIViewController new method that Apple added after IOS7, and that the purpose of this is to give developers more flexibility in customizing each ViewController status bar.

-(BOOL) prefersstatusbarhidden{return
  YES;

IOS7 UIViewController not only provides this function for setting the status bar, but also the rest, in detail later.

Method Two: Through Info.plist control

1, first we still have to set this (in the 2nd step of the two ways to set this parameter)

2, then set (two ways)

Or

The two are equivalent! And the state of both is synchronized.

Second, status bar style

Take a look at all the styles (explain the comments)

typedef ns_enum (Nsinteger, Uistatusbarstyle) {
//default style, black-text transparent status bar, suitable for pages with a bright background color
  uistatusbarstyledefault                   = 0,//Dark content, for using on light backgrounds
//white transparent status bar, suitable for pages with dark background color
  uistatusbarstylelightcontent   ns_ Enum_available_ios (7_0) = 1,//Light content, for use on dark backgrounds

//iOS7.0 before black background white, iOS7 later with Uistatusbarstylelig Htcontent effect like
  uistatusbarstyleblacktranslucent Ns_enum_deprecated_ios (2_0, 7_0, "use Uistatusbarstylelightcontent ") = 1,
//iOS7.0 Previous startup page is Gray White, iOS7 after the same as
  the uistatusbarstylelightcontent effect Uistatusbarstyleblackopaque   Ns_enum_deprecated_ios (2_0, 7_0, "use uistatusbarstylelightcontent") = 2,
} __ tvos_prohibited;

How to set the status bar style

Setting The Statusbarstyle does nothing if your application is using the default uiviewcontroller-based status bar Syst EM.
@property (ReadWrite, nonatomic) uistatusbarstyle statusbarstyle 
Ns_deprecated_ios (2_0, 9_0, "use-[ Uiviewcontroller Preferredstatusbarstyle] ") __tvos_prohibited;
-(void) Setstatusbarstyle: (Uistatusbarstyle) Statusbarstyle animated: (BOOL) animated 
Ns_deprecated_ios (2_0, 9_0 , "Use-[uiviewcontroller Preferredstatusbarstyle]") __tvos_prohibited;

Likewise iOS9 after these methods were disabled, Apple recommended in the concrete viewControllerUse -[UIViewController preferredStatusBarStyle]

-(Uistatusbarstyle) preferredstatusbarstyle{return
  uistatusbarstylelightcontent;
}

Note: What we usually use viewController is nested in, where UINavigationController viewController using a - (UIStatusBarStyle)preferredStatusBarStyle; function will find that the setting does not take effect.

The system also provides us with a function - (UIViewController *)childViewControllerForStatusBarStyle that can also solve the problem, which we'll talk about later.

Third, background color

IOS7 the background of the status bar is transparent in the future by default, one way is to write a UIView background to add to the status bar, so you can set the color of the status bar at will.

Another way is navigationBar setBarTintColor to change the status bar color by setting the color

Other functions related to the status bar in Uiviewcontroller

preferredstatusbarupdateanimation function

-(uistatusbaranimation) preferredstatusbarupdateanimation 
Ns_available_ios (7_0) __tvos_prohibited; Defaults to Uistatusbaranimationfade

If you want to change the style of the status bar in the current display UIViewController , you need to call the above function. When this function is called, the system calls the method to redraw preferredStatusBarStyle the style of the status bar actively

Childviewcontrollerforstatusbarstyle function

Override to return a child view controller or nil. If Non-nil, that view controller ' s status bar appearance attributes'll be used. If nil, self is used. Whenever the return values from this methods change,-setneedsupdatedstatusbarattributes should to be called.
-(Nullable Uiviewcontroller *) Childviewcontrollerforstatusbarstyle 
Ns_available_ios (7_0) __tvos_prohibited;

The return value of this function is returned by default nil , at which point the system will call the current function, and viewControllerA if the preferredStatusBarStyle return value is another viewControllerB then the system will call viewControllerB the preferredStatusBarStyle function.

Use this function to solve UINavigationController the problem of invalid nested settings style.

Explain why the nested UINavigationController viewController preferredStatusBarStyle function settings are not valid:

When we're nesting UINavigationController , we're AppDelegate.window.rootViewController usually created, and the navigationController first thing we call is the navigationController function in, because the childViewControllerForStatusBarStyle default returns nil , then navigationController we call the preferredStatusBarStyle function itself, so we viewController The preferredStatusBarStyle status bar style set through the function in is not called to be discovered, so it is invalid.

So we're going to create an inherited UINavigationcontroller NavigationController , overriding function in this subclass. childViewControllerForStatusBarStyle

-(Uiviewcontroller *) childviewcontrollerforstatusbarstyle{return
  Self.topviewcontroller;
}

So that navigationController the childViewControllerForStatusBarStyle function in this will return to the top of the class navigationController viewController , then the set of viewController functions in the preferredStatusBarStyle system will be informed

Childviewcontrollerforstatusbarhidden function

-(Nullable Uiviewcontroller *) Childviewcontrollerforstatusbarhidden 

Ns_available_ios (7_0) __tvos_prohibited;

childViewControllerForStatusBarHiddenfunction of the use of the same principle, no longer repeat.

preferredstatusbarupdateanimation function

Override to return the type of animation that should is used for status bar changes for this view controller. This is currently only affects changes to Prefersstatusbarhidden.
-(uistatusbaranimation) preferredstatusbarupdateanimation 
Ns_available_ios (7_0) __tvos_prohibited;//Defaults To Uistatusbaranimationfade

The animation form is as follows

typedef ns_enum (Nsinteger, uistatusbaranimation) {
  uistatusbaranimationnone,
  uistatusbaranimationfade NS_ Enum_available_ios (3_2),
  uistatusbaranimationslide Ns_enum_available_ios (3_2),
__tvos_prohibited;

This function returns the animation effect. The animation effect is prefersStatusBarHidden displayed only when the function returns a value, and by calling the
[self setNeedsStatusBarAppearanceUpdate]function to redraw the status bar

Four, the application

We can UIWindow implement a custom status bar by hiding the system status bar and then customizing it by setting setWindowLevel:UIWindowLevelStatusBar .

Summarize

This is the full content of this article, I hope you can help iOS developers, if you have questions you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.