IOS implements a custom navigation bar background and a custom color status bar (supports 7.0 and lower versions)

Source: Internet
Author: User

To respect the original author of the article, reprint must indicate the original address: http://www.cnblogs.com/wt616/p/3784717.html

Look first:

When customizing the navigation bar background, you may experience some of the following issues:

1. When the navigation bar background is set, the color of the status bar will change along with it, which may not be what you said you wanted to see.

2, IOS7 more than the version and the lower version of the navigation bar displayed the height of the difference, this difference is the status bar height 20, in order to be compatible with the lower version, must be unified

Solution Ideas:

1, abnormal is the status bar background has also changed, and the status bar text can be set through other APIs: such as [[uiapplicationsharedapplication] Setstatusbarstyle: Uistatusbarstylelightcontent];

2, the text is attached to the background above, so consider inserting a rectangular color between this block (even the picture can)

3, according to this idea, the second way of thinking is also produced, is to produce a 20+44=64 height of the picture, as the navigation bar background, where the top 20 is the color of the status bar.

4, this article realizes the first kind of thought, and the simulator and the real machine as well as the ios6,7 have verified the feasible

More than 7.0 versions solve high-rise problems with a single line of code

if ([[[Uidevicecurrentdevice] systemversion] doublevalue]>=7.0) {

self. Edgesforextendedlayout=uirectedgenone;

}

Create a false status bar background with a height of 20

UIView *statusbarview = [[UIView alloc] initwithframe:cgrectmake (0,---- )];

Set it to the color you need, here I choose black, I am very calm

Statusbarview. backgroundcolor=[uicolor blackcolor];

Here my idea is: before the ideal state is the status bar color has become the color of the navigation bar, but according to this situation, but instead help me to determine that the status bar at this time is also part of the navigation bar, and the status bar text floating above, So, in theory, adding a subview directly to the navigation bar is the middle layer of them.

Push the code like this:

[self. Navigationcontroller. Navigationbar addsubview: Statusbarview];

Modify the navigation bar text color, here I choose White, that I am very pure

[[uiapplicationsharedapplication] setstatusbarstyle: uistatusbarstylelightcontent];

Set the background picture of the navigation bar

[self. Navigationcontroller. Navigationbarsetbackgroundimage: [uiimageimagenamed:@ "bg.jpg"] forbarmetrics: Uibarmetricsdefault];

IOS implements a custom navigation bar background and a custom color status bar (supports 7.0 and lower versions)

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.