iOS development custom default generated navigation bar title color Back button

Source: Internet
Author: User

Change the navigation bar color navigation bar in which page code is placed in the face

  self.navigationController.navigationBar.tintColor = [Uicolor colorwithred: (21.0/ 255.0) Green: (153.0/255.0) Blue: (224.0/255.0) alpha:1];                                                                                                      //define navigation bar color

self.navigationItem.title = @ "Custom"; / / Define the title of the navigation bar

The second navigation bar comes with a back button, and we need to customize its style, which can be a lot of ways. For example, the view hierarchy of the Hack navigation bar. If you don't want to Hack the navigation bar, you can use Navigationbardelegate. The problem is that if the navigation controller comes with Navigationbar, you will not be able to access Navigationbar (the program will crash). Here is the description in the Apple documentation:

Note that if you use Auinavigationcontroller object to manage hierarchical navigation, you should notdirectly access the N Avigation Bar object.

Here, we offer another "custom" approach. It may not be called customization, because we actually hide the default return button and provide a custom return button as the LeftButton of the navigation bar. Using this method, we can customize not only the style of the button (title and background image), but also the custom method. The default Return button action is Popviewcontroller, which we can modify for other actions.

This process is roughly divided into 4 steps:

1. Hide the default return button, which is done by setting Navigationitem Hidesbackbutton for Yes:

Hide default "Back" button

[Self.navigationitemsethidesbaCkbutton:yes];


2, customize a barbuttonitem. First, we customize a UIButton. This UIButton is initialized with the Buttonwithtype:uibuttontypecustom method. Then use the Setbarckgroundimage method to customize the background image of the button, using the Addtarget method to specify the event handling method of the button. This allows us to obtain a fully customizable button. The Barbuttonitem has a Initwithcustomview: Initialization method. We can use a custom view (such as our custom button) as the parameter of this method to build a barbuttonitem.


Customize the Back button of the navigation bar

UIBUTTON*BTN = [Uibuttonbuttonwithtype:uibuttontypecustom];

Btn. Frame=cgrectmake (15, 5, 38, 38);

[Btn setbackgroundimage:[uiimage imagenamed:@ "return. png"] forstate:uicontrolstatenormal];

[Btn addtarget:selfaction: @selector (gobackaction) forcontrolevents:uicontroleventtouchupinsIDE];

UIBARBUTTONITEM*BACK=[[UIBARBUTTONITEMALLOC]INITWITHCUSTOMVIEW:BTN];


3, set the Barbuttonitem to Navigationitem Leftbarbutton.


Set the LeftButton of the navigation bar

Self.navigationitem.leftbarbuttonitem=back;


4. Write the event code of the button.

-(void) gobackaction{

Add a custom action to the return button here

[SELF.NAVIGATIONCONTROLLERPOPVIewcontrolleranimated:yes];

}

Transferred from: http://blog.sina.com.cn/s/blog_a5ef060c01013946.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS development custom default generated navigation bar title color Back button

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.