"iOS Dev-22" Navigationbar navigation bar and Navigationitem settings: basically fix the text and buttons on the navigation bar and various jumps

Source: Internet
Author: User

(1) Navigationbar navigation bar can be regarded as a property of the Self.navigationcontroller navigation controller, can be directly used to represent Self.navigationController.navigationBar, of course navigation Bar itself has a lot of properties, such as style barstyle, background backgroundcolor, frame properties (can get the information of wide height), you can also use the SetBackgroundImage method to set the background picture, Of course the picture is much more can be cut using clipstobounds.


(2) But, navigationbar whether 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 method Setnavigationbarhidden, all can achieve effect.


(3) Another important knowledge is the setting of the Navigationitem, this attribute and navigationcontroller are lateral, so can be used directly with 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 in the middle of the navigation bar, using the method is Settitleview, many of the game's navigation bar in the middle seems to be a picture, you can use this.


(4) The most important thing is to set the left and right buttons on the Navigationitem, usually the default is "back" on the left side, on the far side there is a "camera" (such as a friend circle). The step is to create a Uibarbuttonitem object and then assign the object directly to Self.navigationItem.leftBarButtonItem or to the right. Of course, you can also create a number of Uibarbuttonitem to form an array, and then assign the array to Self.navigationItem.leftBarButtonItems, notice that this is more than the previous one "s", there are many. Also notice the order in which multiple buttons are arranged.


(5) We create these navigation bar buttons have many forms, some are by the text, some when the picture, sometimes the system comes with such as camera or reply these icons, and some are entirely their own definition of the view. We can of course also use the navigation bar buttons that we created to overwrite the default buttons generated by the original navigation controller, such as "<back".


Similarly, you need to create two view controllers (Viewcontroller root view controller, Secondviewcontroller Child View Controller), and then place it in the navigation controller stack. And 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:uibuttonty    Peroundedrect];    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 there is 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]; You can use Self.navigationController.navigationBar.frame.size to get a high width and self.navigationcontro.Ller.navigationBar.frame.origin get x and y//height 44, Width 375, if it is Retina screen, then width and height @2x can be 750 and//x is 0 is obvious, Y is 20, where 20 is left to the status bar of the high NS        Log (@ "%f", SELF.NAVIGATIONCONTROLLER.NAVIGATIONBAR.FRAME.ORIGIN.Y);    Hide the navigation bar, then click into other views when the navigation bar will also be hidden, the default is no//the following one directly to the Navigationbarhidden assignment, a call method, is the same, the following one more than an animation option    Self.navigationcontroller.navigationbarhidden=no;    [Self.navigationcontroller Setnavigationbarhidden:no Animated:yes]; Add a background image to the navigation bar, where forbarmetrics is somewhat similar to the button's for state status, that is, what state displays//uibarmetricsdefault-vertical screen, horizontal screen navigation bar becomes wider, then automatically repeat picture// uibarmetricscompact-vertical Screen No, horizontal screen has, equivalent to the previous version of the old iOS 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, later encountered in detail [Self.navigationController.navigationBar setbackgroundimage:[uiimage    imagenamed:@ "Big2.png"] forbarmetrics:uibarmetricsdefault]; If the picture is too general to expand the position of the encroachment status bar, the//clipstobounds displayed below the status bar 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 load any one view//view of x and Y is invalid, the view is centered around the top and bottom of the title UIView *textview1=[[uiview Alloc]initwithframe:cgrectmake (    10, 10, 50, 30)];    Textview1.backgroundcolor=[uicolor Whitecolor];        [Self.navigationitem Settitleview:textview1]; Set the left and right buttons of the navigation bar//Instantiate first create a uibarbuttonitem, then assign this button to Self.navigationItem.leftBarButtonItem The type of button that initializes the text has Uibarbuttonitemstyleplain and uibarbuttonitemstyledone two types, the difference looks not very uibarbuttonitem *barbtn1=[[ Uibarbuttonitem alloc]initwithtitle:@ "left" Style:uibarbuttonitemstyleplain target:self action: @selector (ChangeColor    )];        SELF.NAVIGATIONITEM.LEFTBARBUTTONITEM=BARBTN1; We can also have more than one button on the left and right, and can add any view to the right as an example//add more than is actually the Rightbarbuttonitems property, note there is a rightbarbuttonitem, The former is given an array of Uibarbuttonitem objects, so multiple can be displayed. 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 to some of the system's own Barbutton, such as Uibarbuttonsystemitemcamera is the camera, and done,reply and so on, will be displayed as an ICThe on icon//can also be initwithimage initialized to a picture//can also be customized, can be any one uiview uibarbuttonitem *barbtn2=[[uibarbuttonitem Alloc]initwithbar    Buttonsystemitem: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 also changed, because only display in the stack top view Controller view//SO ( 1) control so-called jump, in fact, is the navigation controller in the control, inside the elements can be obtained through the Navigationcontroller property to their navigation controller//SO (2) get to the navigation controller, using the Push method, Put a view controller into the stack senCon1, this new put in the top of the stack, it shows its view, so the user changed 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 customize a Back button in the sub-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 and Navigationitem settings: basically fix the text and buttons on the navigation bar and 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.