iOS development itself defines the default generated navigation bar title color returned by 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 = @ "own definition"; / / Define the title of the navigation bar

The second navigation bar comes with a return button, and we need to customize its style. There are many ways to do this.

For example, Hack the view hierarchy of the navigation bar. Suppose you don't want to Hack the navigation bar. Then you can use Navigationbardelegate.

The problem is that the assumption is the navigationbar that the navigation controller comes with. 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 a "custom" approach.

may not be called customization. Since we are actually hiding the default return button, we provide a leftbutton that defines the return button as the navigation bar. Using this method, we can not only customize the button's style (title and background image). And can trigger a method of its own definition. The default Return button action is Popviewcontroller, which we can change to 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 the default return button

[Self.navigationitemsethidesbaCkbutton:yes];


2, define a barbuttonitem. First, we customize a UIButton. This UIButton is initialized with the Buttonwithtype:uibuttontypecustom method. Then use the Setbarckgroundimage method to customize the button's background image. Use the Addtarget method to specify the event-handling method for the button. So we get a completely customizable button. The Barbuttonitem has a Initwithcustomview: Initialization method. We were able to construct a barbuttonitem with a custom view (for example, our custom button) as a reference to this method.


Define the "Back" button for your 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 self-defined action that returns a button here

[SELF.NAVIGATIONCONTROLLERPOPVIewcontrolleranimated:yes];

}

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

iOS development itself defines the default generated navigation bar title color returned by button

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.