Customize the navigation bar: Background color settings, button caption picture settings, picture coordinate modification

Source: Internet
Author: User

First, modify the system native navigation bar to modify the navigation bar background color
Self.navigationController.navigationBar.barTintColor = [Uicolor colorwithhexstring:@ "#2295f2 "];

Custom navigation bar Buttons
Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@ "  Btn-menu-h"] style:uibarbuttonitemstyleplain target:self action: @selector (Menubtnclick)];

The custom button picture is too far from the edge of the screen and can be modified by the following code

Modified Rightbarbuttonitem margin

system default Rightbarbuttonitem margin
Self.navigationItem.leftBarButtonItem.imageInsets = Uiedgeinsetsmake (0,-0,0 = Uiedgeinsetsmake (0,-ten,0,ten); 

Setting the background color of the navigation bar will cause the button title color to change, modify the following method
Self.navigationController.navigationBar.tintColor = [Uicolor Whitecolor];

Modify title color and font size
self.navigationController.navigationBar.titleTextAttributes [email Protected]{nsforegroundcolorattributename: [ Uicolor colorwithhexstring:@ "#ffffff"],nsfontattributename:[uifont systemfontofsize: []};

Modify button title Color size
[Self.navigationItem.rightBarButtonItem settitletextattributes:@{nsforegroundcolorattributename: [Uicolor Colorwithhexstring:@ "#ffffff"],nsfontattributename:navitermfont} forstate: UIControlStateNormal];

Because the navigation bar is translucent, if you do not handle the color of the navigation bar and the design color is always error this line of code can turn off the translucent effect, but will cause the coordinates 0 points to move.
[Uinavigationbar appearance].translucent = NO;

Turn off coordinates 0 point move
Self.edgesforextendedlayout = Uirectedgenone;

For more information about the Translucent property, please visit: IOS7 navigation bar Adaptation to the--translucent property setting. Custom navigation bars If the system navigation bar does not meet the design requirements, it needs to be customized according to the design, only the title and left and right buttons are written in the code. You can add any control according to your design.
#import @interfaceCustomnav:uiview@property (nonatomic,strong) UIButton*Buttonleft, @property (nonatomic,strong) UIButton*Buttonright;- (ID) Initwithtitle: (NSString *) title;@end#import "CustomNav.h"@implementationCustomnav- (ID) Initwithtitle: (NSString *) title{ Self=[Super init];if(self) {self.frame= CGRectMake (0,0, Screen_width, -); # #标题 UILabel*titlelabel =[[UILabel alloc]init]; Titlelabel.frame= CGRectMake (screen_width/4.0, A, screen_width/2.0, +); Titlelabel.textalignment=Nstextalignmentcenter; Titlelabel.textcolor= [Uicolor colorwithhexstring:@"#ffffff"]; Titlelabel.font= [Uifont systemfontofsize: -]; Titlelabel.text=title;    [Self Addsubview:titlelabel]; # #左按钮 Self.buttonleft=[UIButton Buttonwithtype:uibuttontypecustom]; Self.buttonLeft.frame= CGRectMake (0, A, +, +);    [Self addSubview:self.buttonLeft]; Self.buttonLeft.titleLabel.font= [Uifont systemfontofsize: the]; Self.buttonLeft.contentHorizontalAlignment=Uicontrolcontenthorizontalalignmentright; # #右按钮 Self.buttonright=[UIButton Buttonwithtype:uibuttontypecustom]; Self.buttonRight.frame= CGRectMake (Screen_width- +, A, +, +);    [Self addSubview:self.buttonRight]; Self.buttonRight.titleLabel.font= [Uifont systemfontofsize: the]; Self.buttonRight.contentHorizontalAlignment=Uicontrolcontenthorizontalalignmentleft; Self.backgroundcolor= [Uicolor colorwithhexstring:@"#2295f2"];}returnSelf ;}@end

Customize the navigation bar: Background color settings, button caption picture settings, picture coordinate modification

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.