Uinavigationcontroller, like Uitableviewcontroller, is one of the most common controls in iOS development, so learn how to use it today. Some people say that TableView is the hardest, because it involves interacting with data, but in my opinion, Uinavigationcontroller should be a relatively complex control, because the development of mobile side, the user experience is the most important, so good visual interaction becomes an essential part , which requires us to work more on the visual level, Uinavigationcontroller inside the coupling of the various properties, indeed some cumbersome, today to take a good look.Uinavigationcontroller is a navigation controller, you can control the jump between different pages, such as what we usually do, click on a button to go to another page, press back and back again, this is the role of the navigation controller, We can imagine that there are more than n pages in a navigation, and these pages are controlled by this navigation. Uinavigationcontroller has several properties, which we understand for the moment to be divided into several layers, each of which isNavigationbar,navigationtoolbar and so on. Now let's start with the creation of Uinavigationcontroller to learn from it. according to our normal logic, the app will have a default rootview when it loads, we usually set the page we need to show as Rootviewcontroller, but now we are navigating to manage our pages, so When the navigation is created, we bind the page that we need to show, in the use of the navigation controller, we must establish the idea is that the page in the navigation, navigation only a concept, instead of thinking about navigation is just a control on the top of the page, so thinking is not conducive to the development of the. So, we first create a uiviewcontroller, we name it Myviewcontroller, in APPDELEFATE.M, we write:Myviewcontroller *mvc= [[Firstviewcontroller alloc]init]; Uinavigationcontroller *nav = [[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:MVC];Self.window.rootViewController = nav;
The root view controller is then set to the navigation controller, which successfully loads the page onto the navigation.
Next we need to define a concept:Navigationbar So what is Navigationbar? We interpret it as a navigation bar. Navigation Controller is a whole concept, specific to what we see intuitively, is the current navigation controller navigation bar, that is, Navigationbar. Also, the navigation bar belongs to the entire navigation, not to a fixed viewcontroller. If we don't need to show navigation, we should set this up:Self.navigationController.navigationBarHidden = YES;in this case, the navigation bar will not be displayed. the same idea to think about the problem, if we want to set the background color of the navigation bar, of course, also to set the color of NavigationbarSelf.navigationController.navigationBar.barTintColor = [Uicolor redcolor];If you want to set the choice of staining, you shouldSelf.navigationController.navigationBar.tintColor = [Uicolor greencolor];Okay, now that we know Navigationbar for the entire navigation, now we need to do our custom operation for the navigation bar of a single page. That's the next concept:Navigationitem For example, I want to set the navigation title of the current page, then I can write this:Self.navigationItem.title = @ "111";
from a theoretical point of view, Navigationitem is a property of the Uiviewcontroller, and it is for the Uinavigationcontroller service, from the official documents given by the interpretation of the view,Navigation item at navigation bar represents a Viewcontroller each viewcontroller added to the Navigationcontroller will have a corresponding navigationitem, Therefore, we can consider this attribute as a unique navigation bar for each page. So, we can also create a label to show our title in the way of title, Titleview can be customized
UILabel *titlelabel = [[Uilabelalloc]initwithframe:cgrectmake (0, 0, 60, 40)];
Titlelabel.text = @ "title";
Titlelabel.textalignment = Nstextalignmentcenter;
Titlelabel.backgroundcolor = [Uicolorwhitecolor];
Self.navigationItem.titleView = Titlelabel;
On our current page of the navigation bar, we often need to create a button, first of all, the navigation bar comes with a dedicated button Uibarbuttonitem
That is, the properties of Navigationitem, generally divided into three kinds: Leftbarbuttonitem,rightbarbuttonitem,backbarbuttonitem , We can customize, pictures, text, System style these styles to create
Uibarbuttonitem *lbn= [[uibarbuttonitemalloc]initwithtitle:@ "LBN" Style:uibarbuttonitemstyleborderedtarget: Selfaction: @selector (Lbnclick)];
Self.navigationItem.leftBarButtonItem = LBN;
Of course, if you need to create more than one dedicated button, then we can add these buttons to an array, and then adding the array as a button.
Navigation also has a property,ToolBar, which is the same as the navigation bar, is for the entire navigation, the default is hidden, we need to set its hidden properties before we can see the effect:
Self.navigationController.toolbarHidden = NO;
The height of the toolbar is 44, at the bottom of the current page. Unlike Tabbar, which sets the current page and Tabbar corresponds to multiple viewcontroller, this line of code defines the toolbar style:[self.navigationcontroller.toolbarsetbackgroundimage:[uiimageimagenamed:@ "HEADER_BG"] forToolbarPosition: Uibarpositionbottombarmetrics:uibarmetricsdefault];As with the navigation bar's dedicated button, we can also create toolbar's dedicated buttons in different ways and then add them to self. The ToolbarItems.Create a button, we can add click events to the button, just follow the normal way to add it. in the navigation, if I click the button to jump to the new page, then we can do this:[Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC animated:yes];
or a
[Self.navigationcontroller Popviewcontrolleranimated:yes];
What is the difference between pop and push, push can be understood to jump to a fixed page, pop is return, Viewcontroller need to be established in advance.
if it is a normal button, the jump page is not in the navigation, we can use present to launch a new page. Jump Back Method:[Self dismissviewcontrolleranimated:<# (BOOL) #> completion:<#^ (void) completion#>];One thing to note is that when you push out a new page, you bring a return button, and if you need to customize it, hide and then customize the Add Private button.
iOS Learning Experience--uinavigationcontroller