iOS Dev-22 navigationbar navigation bar, Navigationitem build: Gets the basic text and button in the navigation bar as well as various jumps

Source: Internet
Author: User

(1) The Navigationbar navigation bar can be viewed as a Self.navigationcontroller property navigation controller, which can be represented directly by a point self.navigationController.navigationBar. Of course Navigationbar He is still very property. Let's style barstyle, background backgroundcolor, frame properties (to get the information wide and high). You can also set a background image using the SetBackgroundImage method. Of course the picture is much more able to use clipstobounds tailoring.


(2) but. Navigationbar whether to hide and show this need it Dad is also self.navigationcontroller to control, there is direct. Navigationbarhidden is set to Yes/no, Can also use the method Setnavigationbarhidden, all can achieve the effect.


(3) There is also an important knowledge of the Navigationitem settings, this attribute and Navigationcontroller is peer, so directly can use Self.navigationitem. Of course, there is a way to set the title of the navigation bar Settitle, of course, you can also directly change the text to a view. That is, the so-called title view is placed in the middle of the navigation bar, using the method is Settitleview, very many game navigation bar in the middle seems to be a picture, can use this.


(4) The most important thing is to set the button on the left and right side of the Navigationitem, usually the default is "back" on the other side. On the right there is a "webcam" (like a circle of friends). The step is to create a Uibarbuttonitem object and then assign the object directly to Self.navigationItem.leftBarButtonItem or to the right. It is also possible to create an array of very many uibarbuttonitem at once. The array is then assigned to the Self.navigationItem.leftBarButtonItems. Note that this is more of an "s" than the previous one. There are very many. Also notice the order in which multiple buttons are arranged.


(5) These navigation bar buttons we create have very many forms. Some are written, sometimes pictures, and sometimes the system comes with a camera or reply these icons, and some are completely custom views.

We can of course also use the navigation bar button we created to overwrite the default button, such as "<back", generated by the original navigation controller.


Same. There is a need to create two view controllers (Viewcontroller root view controller, Secondviewcontroller Child View Controller), and then place it in the navigation controller stack. Furthermore, the navigation controller is assigned to Self.window.rootViewController in APPDELEGATE.M.

In the VIEWCONTROLLER.M:

#import "ViewController.h" #import "SecondViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) Viewdidload {//Create a button, click to enter child View controller, equivalent to enter sub-page UIButton *btn1=[uibutton Buttonwithtype:uibutt    Ontyperoundedrect];    Btn1.frame=cgrectmake (38, 100, 300, 30);    [Btn1 settitle:@ "Jump to Secondviewcontroller" forstate:uicontrolstatenormal];    Btn1.backgroundcolor=[uicolor Whitecolor];    Self.view.backgroundcolor=[uicolor Redcolor];    [Btn1 addtarget:self Action: @selector (Jumpto) forcontrolevents:uicontroleventtouchupinside];    [Self.view ADDSUBVIEW:BTN1]; Set the navigation bar style//default white translucent (a little gray feeling), uibarstyleblack,uibarstyleblacktranslucent,uibarstyleblackopaque are black translucent.    In fact, they are not transparent when there is transparent when the translucent, but do not know why no effect self.navigationcontroller.navigationbar.barstyle=uibarstyledefault;    Set the navigation bar background color, also translucent glass-like color effect self.navigationcontroller.navigationbar.backgroundcolor=[uicolor Orangecolor]; Able to use Self.navigationController.navigationBar.frame.size to get a high width and self.navigationcOntroller.navigationBar.frame.origin get x and Y//h 44. Width 375, assuming that the retina screen, then width and height @2x can be respectively 750 and the//x is 0 very obvious, Y is 20.        Above 20 is left to the status bar height NSLog (@ "%f", SELF.NAVIGATIONCONTROLLER.NAVIGATIONBAR.FRAME.ORIGIN.Y); Hides the navigation bar, which is also hidden when you click into other views. The default is no//the following one directly to the Navigationbarhidden assignment, a call method, are the same, the following one is more than one animation options self.navigationcontroller.navigationbarhidden=    NO;    [Self.navigationcontroller Setnavigationbarhidden:no Animated:yes]; Add a background image to the navigation bar, in which Forbarmetrics is somewhat similar to the for state status of the button, that is, what state displays the//uibarmetricsdefault-vertical screen. The horizontal navigation bar becomes wider. Then take the initiative to repeat Picture//uibarmetricscompact-vertical screen, horizontal screen has, equivalent to the previous old iOS version number Uibarmetricslandscapephone// Uibarmetricscompactprompt and Uibarmetricsdefaultprompt temporarily do not know the usefulness. The official explanation is applicable only in bars with the prompt property, such as Uinavigationbar and Uisearchbar, and then in detail after encountering [self.navigatio    Ncontroller.navigationbar setbackgroundimage:[uiimage imagenamed:@ "Big2.png"] forbarmetrics:uibarmetricsdefault]; Assuming the picture is too general to expand the position of the encroachment status bar, the display below the status bar//clipstobounds is to cut out the extra picture Self.navigationcoNtroller.navigationbar.clipstobounds=yes;        Set the navigation title [Self.navigationitem settitle:@ home page]; Set the navigation title view, that is, this piece can be loaded into a random view//view of the X and y invalid.    The view is centered around the top and bottom in the title position UIView *textview1=[[uiview Alloc]initwithframe:cgrectmake (10, 10, 50, 30)];    Textview1.backgroundcolor=[uicolor Whitecolor];        [Self.navigationitem Settitleview:textview1]; Set the left and right button of the navigation bar//Instantiate to create a uibarbuttonitem, then assign the button to Self.navigationItem.leftBarButtonItem. The type of button that initializes the text has Uibarbuttonitemstyleplain and uibarbuttonitemstyledone two types, and the difference looks small uibarbuttonitem *barbtn1=[[ Uibarbuttonitem alloc]initwithtitle:@ "left" Style:uibarbuttonitemstyleplain target:self action: @selector (ChangeColor    )];        SELF.NAVIGATIONITEM.LEFTBARBUTTONITEM=BARBTN1; We can also add more than one button to the left and right. , and can join the random view, to the right for example//add more than the fact is the Rightbarbuttonitems attribute, note another rightbarbuttonitem, the former is given an array of Uibarbuttonitem objects. So can display multiple. The latter is assigned a Uibarbuttonitem object, so only one///display order can be displayed, left: From left to right in array order, right: in array order from right to left//can be initialized into some Barbutton that the system comes with, For example UIBARBUTTONSYSTEMITEMCAMera is a camera, and done. Reply and so on, will be displayed as an icon//can also be initwithimage initialized into a picture//can also be defined by themselves. Can be casual a uiview uibarbuttonitem *barbtn2=[[uibarbuttonitem alloc]initwithbarbuttonsystemitem:    Uibarbuttonsystemitemcamera target:self Action: @selector (CHANGECOLOR2)]; Uibarbuttonitem *barbtn3=[[uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@ "[email protected]"]    Style:uibarbuttonitemstyleplain target:self Action: @selector (ChangeColor3)];    UIView *view4=[[uiview Alloc]initwithframe:cgrectmake (10, 10, 20, 20)];    View4.backgroundcolor=[uicolor Blackcolor];    Uibarbuttonitem *barbtn4=[[uibarbuttonitem ALLOC]INITWITHCUSTOMVIEW:VIEW4];    Nsarray *arr1=[[nsarray alloc]initwithobjects:barbtn2,barbtn3,barbtn4, nil];    SELF.NAVIGATIONITEM.RIGHTBARBUTTONITEMS=ARR1;    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) changecolor{self.view.backgroundcolor=[uicolor purplecolor];} -(void) changecolor2{Self.view.backgroundcolor=[uicolor whitEColor];} -(void) changecolor3{self.view.backgroundcolor=[uicolor orangecolor];} -(void) jumpto{//The core of the two, the so-called jump, in fact, to the navigation controller stack push or pop a view controller, so the top of the view controller is changed, so the view is also changed, because only display in the stack top view Controller view// So (1) control the so-called jump. In fact, the navigation controller is in control, the elements inside can be obtained through the Navigationcontroller property to their navigation controller//SO (2) after acquiring the navigation controller, using the push method, put a view controller senCon1 inside the stack, This new put in the top of the stack.    The view is displayed, so the user changes the page to jump Secondviewcontroller *sencon1=[[secondviewcontroller Alloc]init]; [Self.navigationcontroller pushviewcontroller:sencon1 animated:yes];} @end

In the SECONDVIEWCONTROLLOR.M:

#import "SecondViewController.h" @interface Secondviewcontroller () @end @implementation secondviewcontroller-(void)    viewdidload {UILabel *label1=[[uilabel alloc]init];    Label1.frame=cgrectmake (38, 80, 300, 30);    Label1.backgroundcolor=[uicolor Whitecolor];    [Email protected] "This is Secondviewcontroller";        [Self.view Addsubview:label1];    UIButton *btn2=[uibutton Buttonwithtype:uibuttontyperoundedrect];    Btn2.frame=cgrectmake (38, 120, 300, 30);    [Btn2 settitle:@ "Backto" forstate:uicontrolstatenormal];    Btn2.backgroundcolor=[uicolor Orangecolor];    [Self.view ADDSUBVIEW:BTN2];        [Btn2 addtarget:self Action: @selector (Backto) forcontrolevents:uicontroleventtouchupinside];        Set the navigation title, the title of the return button at this time is the title of the previous level Navigationitem [Self.navigationitem settitle:@ "sub-page"]; We can also define a return button in the child page to overwrite the original "<back" Uibarbuttonitem *barbtn5=[[uibarbuttonitem alloc]initwithtitle:@ "Home" Style:uibarbuttonitemstyleplain target:self Action: @selector (Backto)];        Self.navigationitem.leftbarbuttonitem=barbtn5;    [Super Viewdidload]; Do any additional setup after loading the view.} -(void) backto{[Self.navigationcontroller poptoviewcontroller:[self.navigationcontroller.viewcontrollers OBJECTATINDEX:0] animated:yes];} @end

Cut a Picture:


iOS Dev-22 navigationbar navigation bar, Navigationitem build: Gets the basic text and button in the navigation bar as well as various jumps

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.