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