Customizing the navigation bar and status bar in iOS 7

Source: Internet
Author: User

This article reprinted to http://www.cocoachina.com/industry/20131104/7287.html

Like most developers, I'm also busy upgrading programs to fit iOS 7. There are a number of changes in the appearance of the latest iOS 7. From the developer's point of view, the navigation bar and the status bar have changed significantly. The status bar is now translucent, which also

"" Reading device

Recently, like most developers, I was busy upgrading my program to fit iOS 7. There are a number of changes in the appearance of the latest iOS 7. From the developer's point of view, the navigation bar and the status bar have changed significantly. The status bar is now translucent, which means that the navigation bar appears behind the status bar. In some cases, the background picture of the navigation bar can be extended to the back of the status bar.

I used to write an article about how to customize the navigation bar. But now it's time to update it. Here's what this article will cover:

The default navigation bar in IOS 7

Set the background color of the navigation bar

Use a background picture in the navigation bar

Customize the color of the return button

Modify the font of a navigation bar title

Modify navigation bar title to picture

Add multiple buttons

Modify the style of the status bar

Hide the status bar

Summarize

The code provided in this article needs to be executed with Xcode 5来. If you're still using an older version of Xcode, upgrade Xcode to Xcode 5 before running the sample.

The default navigation bar in IOS 7

Before you start customizing, let's look at the appearance of the default navigation bar in iOS 7. Create a project from Xcode with a single View controller template. The view controller is then embedded in a navigation controller. If you don't want to start from scratch, you can also download this sample project here. Xcode 5 includes iOS 6 and iOS 7 emulators, and we can run a sample program in these two different emulator versions, as shown in the following:

As shown, the navigation bar in iOS 7 is intertwined by default with the status bar, and its color is also modified to light gray.

Set the background color of the navigation bar

In iOS 7, you no longer use the Tintcolor property to set the color of the navigation bar, but instead use the Bartintcolor property to modify the background color. We can didfinishlaunchingwithoptions the method in the APPDELEGATE.M file: Add the following code to modify the color:

    1. [[Uinavigationbar appearance] Setbartintcolor:[uicolor Yellowcolor]];

The effect is as follows:

In general, we will use our own color, the following macro to set the RGB color is very convenient:

    1. #define UICOLORFROMRGB (rgbvalue) [Uicolor colorwithred: ((float) ((Rgbvalue & 0xFF0000) >>))/255.0 Green: (( float) ((Rgbvalue & 0xff00) >> 8)/255.0 Blue: ((float) (Rgbvalue & 0xFF))/255.0 alpha:1.0]

Put the above macro into the APPDELEGATE.M file, and then use this macro to create a Uicolor object (based on the specified RGB). The following example:

    1. [[Uinavigationbar appearance] Setbartintcolor:uicolorfromrgb (0X067AB5)];

By default, the translucent property of the navigation bar is yes. In addition, all navigation bars are obfuscated to make the navigation bar color more saturated in iOS 7. For example, the translucent value is the contrast between no and yes:

To disable the Translucent property, you can select the navigation bar in storyboard and then, in attribute inspectors, uncheck the translucent.

Use a background picture in the navigation bar

If you want to use a picture as the background in the navigation bar, you need to provide a slightly higher picture (which can be extended to the back of the navigation bar). The height of the navigation bar has changed from points (pixels) to points (pixels). We can still use the SetBackgroundImage: method to set a custom picture for the navigation bar. As shown in the following code:

    1. [[Uinavigationbar appearance] setbackgroundimage:[uiimage imagenamed:@"Nav_bg.png"] forbarmetrics: Uibarmetricsdefault];

Two background images are available in the sample project: Nav_bg.png and Nav_bg_ios7.png. Run for a try, the following effect:

Customize the Back button of the Yan

In iOS 7, all the buttons are borderless. The return button will have a V-shaped arrow and a caption from the previous screen ("Back" if the title of the previous screen is empty). To color the return button, you can use the Tintcolor property. As shown in the following code:

    1. [[Uinavigationbar appearance] Settintcolor:[uicolor Whitecolor]];

In addition to the return button, the Tintcolor property affects all button titles and pictures.

If you want to replace the V-shape with your own picture, you can set the Backindicatorimage and backindicatortransitionmaskimage of the picture. As shown in the following code:

    1. [[Uinavigationbar appearance] setbackindicatorimage:[uiimage imagenamed:@"Back_btn.png"];
    2. [[Uinavigationbar appearance] setbackindicatortransitionmaskimage:[uiimage imagenamed:@"Back_btn.png"];

The color of the picture is controlled by the Tintcolor property.

Modify the font of a navigation bar title

As with iOS 6, we can use the Titletextattributes property of the navigation bar to customize the text style of the navigation bar. Use the following keys in the text attributes dictionary to specify the font, text color, text shadow color, and Text shadow offset:

uitextattributefont– Font Key

uitextattributetextcolor– Text color key

uitextattributetextshadowcolor– Text Shadow color key

uitextattributetextshadowoffset– Text Shadow offset key

The title style of the navigation bar is modified as shown in the following code:

  1. Nsshadow *shadow = [[Nsshadow alloc] init];
  2. Shadow.shadowcolor = [Uicolor colorwithred:0.0 green:0.0 blue:0.0 alpha:0.8];
  3. Shadow.shadowoffset = Cgsizemake (0, 1);
  4. [[Uinavigationbar appearance] Settitletextattributes: [Nsdictionary Dictionarywithobjectsandkeys:
  5. [Uicolor colorwithred:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], Nsforegroundcolorattributename,
  6. Shadow, Nsshadowattributename,
  7. [Uifont fontwithname:@"Helveticaneue-condensedblack" size:21.0], Nsfontattributename, nil]];

The effect is as follows:

Modify navigation bar title to picture

If you want to change the title of the navigation bar to a picture or logo, you only need to use the following line of code:

    1. Self.navigationItem.titleView = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Appcoda-logo.png"];

The above code simply modifies the Titleview property and assigns a picture to it. Note: This is not a new feature in iOS 7 and the previous version of iOS can already be available. The exact effect is as shown:

Add multiple buttons

Again, this technique is not iOS 7, and developers often add multiple buttons to the navigation bar, so I decided to introduce them here. We can add multiple buttons to the left or right of the navigation bar. For example, we want to add a camera and a share button to the right of the navigation bar, just use the following code:

    1. Uibarbuttonitem *shareitem = [[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemaction target : Self Action:nil];
    2. Uibarbuttonitem *cameraitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemcamera Target:self Action:nil];
    3. Nsarray *actionbuttonitems = @[shareitem, Cameraitem];
    4. Self.navigationItem.rightBarButtonItems = Actionbuttonitems;

The following effects:

Modify the style of the status bar

In older versions of iOS, the status bar is always in white style. In iOS 7, we can modify the appearance of the status bar in each view controller. The Uistatusbarstyle constant allows you to specify that the contents of the status bar are dark or bright colors. By default, the status bar displays a dark color. That is, the time on the status bar, the battery indicator, and the Wi-Fi signal are displayed as dark colors. If a dark color is used as the background in the navigation bar, the effect looks as follows:

In this case, we might want to change the style of the navigation bar to bright colors. There are two ways to do this. In iOS 7, we can Overridingpreferredstatusbarstyle: methods in each view controller as follows:

    1. -(Uistatusbarstyle) Preferredstatusbarstyle
    2. {
    3. return uistatusbarstylelightcontent;
    4. }

The effect of the above code is as follows:

In iOS 7, using the method above to modify the status bar style is very good. In addition, we can use the UIApplication Statusbarstyle method to set the status bar, but first you need to stop using the view controller-based status bar appearance. In the Info tab of Project target, insert a new key named View controller-based status bar appearance and set its value to No.

You can then use the following code to set the status bar style:

    1. [[UIApplication sharedapplication] setstatusbarstyle:uistatusbarstylelightcontent];

Hide the status bar

Sometimes we need to hide the status bar, so at this point we override the method Prefersstatusbarhidden in the View controller: You can, as shown in the following code:

    1. -(BOOL) Prefersstatusbarhidden
    2. {
    3. return YES;
    4. }

Summarize

IOS 7 gives developers some new degrees of freedom to customize the look of the navigation bar and status bar. I hope the above tips will be useful to you. Here you can download to sample project source code. You only need to cancel the relevant code comment to test it.

Source: The Broken Ship blog

Original: Customizing Navigation bar and Status bar in IOS 7

Customizing the navigation bar and status bar in iOS 7

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.