Original URL: http://www.jianshu.com/p/f0d3df54baa6
Uinavigationbar is a control we often use during the development process, and I'll introduce some common uses for you.
1. Set the title of the navigation bar
That's not much to say, directly on the code
self.navigationItem.title = @"UINavigationBar使用总结";
2. Set the background color of the navigation bar
//通过barTintColor来设置背景色 self.navigationController.navigationBar.barTintColor = [UIColor redColor];
The results are as follows:
Snip20150912_1.png
Bartintcolor: This attribute needs to be used above iOS7; If you want to support IOS6 and the following systems, refer to this article: Uinavigationbar Background Color
3. Set the background picture of the navigation bar
In addition to changing the appearance of the navigation bar by setting the background color, we can also set the appearance of the navigation bar with a background image.
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Background"] forBarMetrics:UIBarMetricsDefault];
Snip20150912_2.png
Here we have to say a little bit about uibarmetrics This enumeration, which is primarily used to control the display of navigation bars in different states. and UIButton's
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state
This method is somewhat similar.
Indicates that the portrait screen is displayed horizontally
Uibarmetricsdefault,
Indicates that it is displayed on a horizontal screen only, and the Uibarmetricslandscapephone effect is the same, but IOS8 has been deprecated.
Uibarmetricscompact,
Uibarmetricsdefaultprompt and Uibarmetricscompactprompt These two I do not know what is the meaning, have to know the friend may give us to popularize a bit.
4. Change the color of the top status bar
As can be seen, after we set the background color or background map, the status bar is still the default black, so it doesn't feel good. Fortunately, the system provides us with two styles of Uistatusbarstyledefault and uistatusbarstylelightcontent for our choice.
- Uistatusbarstyledefault, system default style, black content, for light background (e.g. white)
- Uistatusbarstylelightcontent white content for dark backgrounds (red, for example)
below to see how the implementation of the mainstream, the implementation of the way is divided into two steps:
- Add a line uiviewcontrollerbasedstatusbarappearancein the project's Info.plist file, select the Boolean type, and set to Yes,xcode to automatically change the name to view controller-based status bar appearance.
Snip20150913_4.png
- Add the following method to your Viewcontroller
-(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}
To find out more, you can refer to these two pages: How to changes status bar text color in IOS 7 and iOS7 status bar font Color modification
Also, it is important to note that if your viewcontroller is coming in via Navigationcontroller push, you need to add the following code to take effect:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
Specifically, refer to Uistatusbarstyle Preferredstatusbarstyle does not work on IOS 7
Well, let's take a look at the running effect.
Snip20150913_5.png5. Set Back button
From the above we can see the return button or the default blue button, below I will introduce you to the personalization of the return button.
- (void) Gotoback {[Self. Navigationcontroller popviewcontrolleranimated:YES];} - (void) Setbackbuttonwithimage {UIImage *leftbuttonicon = [[UIImage imagenamed:@ "Leftbutton_back_icon"] imagewithrenderingmode: Uiimagerenderingmodealwaysoriginal]; uibarbuttonitem *leftbutton = [[UIBarButtonItem alloc] Initwithimage:leftbuttonicon style:uibarbuttonitemstylebordered target:self action: @selector (Gotoback)]; self.navigationitem//repair Navigationcontroller slide off failure problem self .navigationcontroller.interactivepopgesturerecognizer< Span class= "hljs-variable" >.delegate = (id) self;}
The results are as follows:
Snip20150915_2.png
There are three points to note here:
- Events that need to implement the return button themselves.
- Special explanation under UIImage's Imagewithrenderingmode: method, Parameter uiimagerenderingmodealwaysoriginal is always rendered with the original, If you do not, the return button will display the color of the Tintcolor (blue by default). The same problem exists in Uitabbaritem.
- We set the return button ourselves, which causes the system to fail the slide-off effect. Add the last line of code in the above code to fix it.
- Sets the text of the return button only
- (void)setBackButtonTitle { UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"取消", nil) style:UIBarButtonItemStylePlain target:self action:@selector(goToBack)]; leftButton.tintColor = [UIColor whiteColor]; self.navigationItem.leftBarButtonItem = leftButton;}
The results are as follows:
Snip20150915_3.png
- Custom Back button
If the above methods do not meet your requirements (for example, you need to set the return button text and pictures), you need to use the uibarbuttonitem initwithcustomview method.
- (void) Setcustomleftbutton {uiview* Leftbuttonview = [[UIView Alloc]initwithframe:CGRectMake (0,0,60,40)];uibutton* LeftButton = [UIButton Buttonwithtype:Uibuttontypesystem]; LeftButton. backgroundcolor = [Uicolor Clearcolor]; LeftButton. frame = Leftbuttonview. Frame; [LeftButton setimage:[UIImage imagenamed:@ "Leftbutton_back_icon"] forstate:UIControlStateNormal]; [LeftButton Settitle:@ "Back" forstate:UIControlStateNormal]; LeftButton. Tintcolor = [Uicolor Redcolor]; LeftButton. Autoresizessubviews =YES; LeftButton. contenthorizontalalignment = uicontrolcontenthorizontalalignmentleft; LeftButton. Autoresizingmask = Uiviewautoresizingflexiblewidth | Uiviewautoresizingflexibleleftmargin; [LeftButton AddTarget: SelfAction:@selector (gotoback) forcontrolevents:UIControlEventTouchUpInside]; [Leftbuttonview Addsubview:leftbutton]; uibarbuttonitem* Leftbarbutton = [[Uibarbuttonitem alloc] Initwithcustomview:leftbuttonview]; Self . Navigationitem. Leftbarbuttonitem = Leftbarbutton;}
Get the following:
Snip20150915_5.png
Set Rightbarbuttonitem basically can not be separated from the above several ways, we may refer to the above to return the button settings.
6. Hide the line at the bottom of the navigation bar
Sometimes there are some special requirements that need to hide the lines at the bottom of the navigation bar.
Two lines of code can do it.
- Set the background map for the navigation bar (SetBackgroundImage method)
- Set the shadowimage of the navigation bar (Setshadowimage method)
UINavigationBar *navigationBar = self.navigationController.navigationBar; //设置透明的背景图,便于识别底部线条有没有被隐藏 [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; //此处使底部线条失效 [navigationBar setShadowImage:[UIImage new]];
Take a look at:
Snip20150922_1.png
In addition, there is a way, a line of code can achieve the effect, it is really amazing.
//方法二: self.navigationController.navigationBar.clipsToBounds = YES;
For more detailed information, refer to this page: How to hide iOS7 uinavigationbar 1px bottom Line
7. Set the color of the line at the bottom of the navigation bar
With the basics above, it's easy to set the color of the navigation bar lines.
First, I made a uiimage classification: through the color to turn into uiimage;
Then, use the above scheme to set the bottom line of the navigation bar.
Color-to-picture code:
@implementationUIImage (Colorimage) + (UIImage *) Imagewithcolor: (Uicolor *) color{cgrect rect = cgrectmake (0.0f, Span class= "Hljs-number" >0.0f, 1.0f, 1.0f); uigraphicsbeginimagecontext (Rect.size); cgcontextref context = uigraphicsgetcurrentcontext (); Span class= "hljs-built_in" >cgcontextsetfillcolorwithcolor (context, [color cgcolor]); Span class= "hljs-built_in" >cgcontextfillrect (context, rect); uiimage *image = Uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); return image;} @end
To set the line color code at the bottom of the navigation bar:
UINavigationBar *navigationBar = self.navigationController.navigationBar; [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; //此处使底部线条颜色为红色 [navigationBar setShadowImage:[UIImage imageWithColor:[UIColor redColor]]];
According to the Convention, look at:
Snip20150923_2.png
Of course there are other ways to do it, such as Addsubview, Addsublayer and so on. If you are interested, you can refer to this page: Ios7-change uinavigationbar border color
8. Add multiple buttons on the navigation bar
As an example of the effect of opening a Web page, there are two buttons: back and close.
. png
There are two options to choose from, but the Leftbarbuttonitems method is ultimately used.
#define USERMETHOD1 0Uibarbuttonitem *closeitem = [[Uibarbuttonitem Alloc] Initwithtitle:@ "Off" style:Uibarbuttonitemstyleplain Target:Self action:@selector (closeaction)];if (USERMETHOD1) {Method One:Self. Navigationitem. Leftbarbuttonitems = @[closeitem];The default return button is required, but the text will show the default back, and I don't know how to change the text.Self. Navigationitem. Leftitemssupplementbackbutton =YES; }else {Method Twouibutton* LeftButton = [UIButton Buttonwithtype:Uibuttontypesystem]; LeftButton. backgroundcolor = [Uicolor Clearcolor]; LeftButton. frame =CGRectMake (0,0,45,40); [LeftButton setimage:[UIImage imagenamed:@ "Leftbutton_back_icon"] forstate:UIControlStateNormal]; [LeftButton Settitle:@ "Back" forstate:UIControlStateNormal]; LeftButton. Tintcolor = [Uicolor Whitecolor]; LeftButton.autoresizessubviews = yes; LeftButton .contenthorizontalalignment = Uicontrolcontenthorizontalalignmentleft; Leftbutton.autoresizingmask = Uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleleftmargin; [LeftButton addtarget:self action: @selector (goToBack) Forcontrolevents:uicontroleventtouchupinside]; uibarbuttonitem* backitem = [[uibarbuttonitem alloc] Initwithcustomview:leftbutton]; self.navigationitem
Then, run the following:
Add multiple buttons on the navigation bar. png
Method one uses leftItemsSupplementBackButton
this property, will show the system default return button, but the text is also showing the default back text, there is no way to find how to modify the text, if anyone knows, but also please enlighten, so I still suggest you use method two. There are also Rightbarbuttonitems This property, you can set this property if you want to display multiple buttons on the right side of the navigation bar.
9. Add a segmented control on the navigation bar
This time, take QQ as an example, the code is as follows:
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"电话"]]; segControl.tintColor = [UIColor colorWithRed:0.07 green:0.72 blue:0.96 alpha:1]; [segControl setSelectedSegmentIndex:0]; self.navigationItem.titleView = segControl;
The code is simple, that is, setting titleView
this property, and of course, you can also set this property to your custom view.
Similar to the navigation bar of QQ. Png10. Navigation bar Global property settings
//全局设置导航栏主题- (void)setNavigationControllerAppearance { [UINavigationBar appearance].barStyle = UIBarStyleBlack; [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithWhite:0.1 alpha:0.5]]; [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];}
There are two benefits of setting the navigation bar globally: one is not to set up each navigationbar, and the other is to make it easy to manage the theme, switch themes, and just change the global settings.
11. Some open source components related to the navigation bar 11.1 njkwebviewprogress-Similar to Safiri progress display when loading a webpage
Page loading progress. png11.2 Fdfullscreenpopgesture-A silky full-screen swipe back gesture
The corresponding article introduction can point this link.
Silky full-screen return gesture. png
Finally, on the demo's address: Navigationbardemo
Text/Falling Feather student (Jane book author)
Original link: http://www.jianshu.com/p/f0d3df54baa6
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
"Go" Uinavigationbar use summary