How to Set buttons, background images, images, and text buttons in the navigation bar

Source: Internet
Author: User

How to Set buttons, background images, images, and text buttons in the navigation bar

The method for setting a button will not be described in detail.

Method 1:

UIBarButtonItem * oneButton = [[UIBarButtonItemalloc] initWithTitle: @ "set" style: UIBarButtonItemStylePlain

Target: selfaction: @ selector (oneButtonAction :)];

UIBarButtonItem * twoButton = [[UIBarButtonItemalloc] initWithTitle: @ "message" style: UIBarButtonItemStyleDone target: selfaction: @ selector (twoButtonAction :)];

NSArray * actionButtonItems = @ [oneButton, twoButton];

Self. navigationItem. rightBarButtonItems = actionButtonItems;

Note: The above style parameters are written in the UIBarButtonItemStylePlain and UIBarButtonItemStyleDone methods. The default UIBarButtonItemStyleDone font is the system button font setting (BOLD ), UIBarButtonItemStylePlain default font is lable font settings (17 characters, no bold)

 

Method 2: (this method is not recommended, because the background of the two buttons (that is, the background of the UIToolbar) will always be displayed)

UIToolbar * tools = [[UIToolbar alloc] initWithFrame: CGRectMake (0, 0, WIDTHINIPHONE6 (95), 44)];

[Tools setBarTintColor: DOMINANTCOLOR];

[Tools setTintColor: WHITEBACK];

[Tools setAlpha: [self. navigationController. navigationBar alpha];

UIBarButtonItem * setBtn = [[UIBarButtonItem alloc] initWithTitle: @ "set" style: UIBarButtonItemStylePlain target: self action: @ selector (setUserSetting)];

UIBarButtonItem * msgBtn = [[UIBarButtonItem alloc] initWithTitle: @ "message" style: UIBarButtonItemStylePlain target: self action: @ selector (lookUserMsg)];

NSArray * btnArr = @ [setBtn, msgBtn];

[Tools setItems: btnArr];

UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithCustomView: tools];

 

2. Background Image

[NavigationController. navigationBar setBackgroundImage: [UIImage imageNamed: @ ""] forBarMetrics: UIBarMetricsDefault];

3. Image and text buttons (here I use the custom left button as an example)

_ LeftBtn = [UIButton buttonWithType: UIButtonTypeCustom];
_ LeftBtn. frame = CGRectMake (0, 0, 60, 25 );
[_ LeftBtn setTitle: @ "return" forState: UIControlStateNormal];
[_ LeftBtn setImage: [UIImage imageNamed: @ "back"] forState: UIControlStateNormal];
_ LeftBtn. titleLabel. font = HEADFONT;
[_ LeftBtn addTarget: self action: @ selector (goback) forControlEvents: UIControlEventTouchUpInside];
_ LeftBtn. imageEdgeInsets = UIEdgeInsetsMake (0,-10, 0, 0 );
_ LeftBtn. titleEdgeInsets = UIEdgeInsetsMake (0,-20, 0, 0 );
UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView: _ leftBtn]; self. navigationItem. leftBarButtonItem = leftButton;

 

Reference: http://blog.csdn.net/lengshengren/article/details/16820803

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.