iOS Uinavigationcontroller navigation bar

Source: Internet
Author: User

1. About the buttons on the left and right sides of the navigation bar

1hide the returned font on the navigation bar//SwiftUibarbuttonitem.appearance (). Setbackbuttontitlepositionadjustment (Uioffsetmake (0, - -), Forbarmetrics:. Default)//OC[[Uibarbuttonitem appearance] Setbackbuttontitlepositionadjustment:uioffsetmake (0, - -) Forbarmetrics:uibarmetricsdefault];//set 2 buttons to the right of the navigation barUibarbuttonitem *item =[[Uibarbuttonitem alloc] Initwithcustomview:view];self.navigationitem.rightbarbuttonitem=item; system default Rightbarbuttonitem margin Self.navigationItem.leftBarButtonItem.imageInsets= Uiedgeinsetsmake (0,- -,0,0); Self.navigationItem.rightBarButtonItem.imageInsets= Uiedgeinsetsmake (0,-Ten,0,Ten);//make left navigation bar button position a bit more left{UIView*left =[[UIView alloc] init]; Left.frame= CGRectMake (0,0,0,1); Uibarbuttonitem*item =[[Uibarbuttonitem alloc] initwithcustomview:left]; //make navigation bar button position more left or rightUibarbuttonitem *nagetivespacer =[[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace Target:nil Action:nil]; Nagetivespacer.width= - +;//This value can be adjusted according to your own needsSelf.navigationItem.leftBarButtonItems=@[nagetivespacer,item];} Uibarbuttonitem*item =[[Uibarbuttonitem alloc] Initwithcustomview:back];item.width= - -;

2. Modify the title

// title color self.navigationController.navigationBar.titleTextAttributes = @{nsforegroundcolorattributename: [Uicolor Somecolor]} // setting the background color of the navigation bar will cause the button title color to change, modify the following method  == [Uicolor colorwithhexstring:@ "#2295f2"];

Set the navigation controller title color and font size, etc.

Nsdictionary *attributes = [Nsdictionary Dictionarywithobjectsandkeys:

[Uicolor Whitecolor],nsforegroundcolorattributename,

[Uifont Systemfontofsize:17],nsfontattributename,nil];

[Self.navigationController.navigationBar settitletextattributes:attributes];

3. Hide the Navigationbar during sliding

(1) Like Safariself.navigationController.hidesBarsOnSwipe=YES; (2)- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{cgfloat OffsetY= Scrollview.contentoffset.y +__tableview.contentinset.top; CGFloat Pantranslationy=[Scrollview.pangesturerecognizer translationinview:self.tableview].y; if(OffsetY > -) {        if(Pantranslationy >0)        {            //downward trend, showing[Self.navigationcontroller setnavigationbarhidden:no Animated:yes]; } Else {            //slide up trend, hide[Self.navigationcontroller Setnavigationbarhidden:yes Animated:yes]; }    } Else{[Self.navigationcontroller setnavigationbarhidden:no animated:yes]; }} The offsety here>64 is only to be processed after the view has been slid over the navigationbar height to prevent effects from being affected. Pantranslationy is the Y-value of the finger position of the scrollview pan gesture, which may not be good because the value of pantranslationy is either positive or negative when it slips up or down the smaller amplitude, which makes the method less sensitive. (3). Hide when our hands leave the screen- (void) Scrollviewwillenddragging: (Uiscrollview *) ScrollView withvelocity: (cgpoint) Velocity targetcontentoffset: (inout Cgpoint *) targetcontentoffset{if(Velocity.y >0) {[Self.navigationcontroller setnavigationbarhidden:yes animated:yes]; } Else{[Self.navigationcontroller setnavigationbarhidden:no animated:yes]; }}velocity.y This amount, in the slip and fall, the change is very small (decimal), but because the direction is different, there are positive and negative points, which is good to deal with. 

4. Set the navigation bar transparency

//the first kind of navigationbar is realized by the gradient of sliding distance- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{cgfloat offsettoshow=200.0;//slide as much as it is completely displayedCGFloat alpha =1-(OFFSETTOSHOW-SCROLLVIEW.CONTENTOFFSET.Y)/offsettoshow; [[Self.navigationController.navigationBar Subviews] Objectatindex:0].alpha =Alpha;}//The second Kind- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{cgfloat offsettoshow=200.0; CGFloat Alpha=1-(OFFSETTOSHOW-SCROLLVIEW.CONTENTOFFSET.Y)/offsettoshow; [Self.navigationController.navigationBar setshadowimage:[uiimageNew]]; [Self.navigationController.navigationBar setbackgroundimage:[self Imagewithcolor:[[uicolor Orangecolor] Colorwithalphacomponent:alpha]] forbarmetrics:uibarmetricsdefault];}//Create a picture of a solid color-(UIImage *) Imagewithcolor: (Uicolor *) color{CGRect rect= CGRectMake (0.0f,0.0f,1.0f,1.0f);    Uigraphicsbeginimagecontext (rect.size); Cgcontextref Context=Uigraphicsgetcurrentcontext ();    Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]);    Cgcontextfillrect (context, rect); UIImage*theimage =Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext (); returnTheimage;} Set Navigation bar Transparency//method One: Set the transparency[[[Self.navigationController.navigationBar Subviews]objectatindex:0] Setalpha:0.1];//method Two: Set the background picture/** * Set the navigation bar to make it transparent **/- (void) Setnavigationbarcolor: (Uicolor *) color Targetcontroller: (Uiviewcontroller *) targetviewcontroller{//the color of the navigation bar and the color of the hidden navigation bar targetViewController.navigationController.navigationBar.shadowImage = [[UIImage alloc]init];CGRect Rect=cgrectmake (0.0f,0.0f,1.0f,1.0f);    Uigraphicsbeginimagecontext (rect.size); Cgcontextref Context=Uigraphicsgetcurrentcontext (); Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]);    Cgcontextfillrect (context, rect); UIImage*theimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); [TargetViewController.navigationController.navigationBar setbackgroundimage:theimage Forbarmetrics: Uibarmetricsdefault];}//3//set an empty picture[Self.navigationController.navigationBar setbackgroundimage:[[uiimage Alloc]init] Forbarmetrics: Uibarmetricsdefault];//clear the border, set an empty picture (hide the bottom shadow bar, pass an empty picture of the UIImage object), the bottom of the black line[Self.navigationController.navigationBar setshadowimage:[[uiimage alloc]init];//is transparentSelf.navigationController.navigationBar.translucent = YES;

5. Swipe back gesture

//turn off Navigationcontroller's swipe back gesture//First Kindself.navigationController.interactivePopGestureRecognizer.enabled =NO;//The second KindIDtarget = Self.navigationController.interactivePopGestureRecognizer.Delegate; Uipangesturerecognizer*pan =[[Uipangesturerecognizer alloc] Initwithtarget:target Action:nil]; [Self.view Addgesturerecognizer:pan];//resolving the navigation controller pop gesture failureSelf.interactivepopgesturerecognizer.Delegate=Self ;-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) gesturerecognizer{//When gestures are valid: When the number of sub-controllers > 1 of the Navigation controller is valid    returnSelf.childViewControllers.count >1;}

6. Hidden display problem in navigation bar (correct posture)

1.//set the agent for the navigation controller to selfSelf.navigationcontroller.Delegate=Self ;2.<uinavigationcontrollerdelegate>3.//going to show the controller- (void) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller Willshowviewcontroller: (UIViewController *) Viewcontroller animated: (BOOL) Animated {//determine if the controller you want to display is your ownBOOL isshowhomepage = [Viewcontroller iskindofclass:[selfclass]]; [Self.navigationcontroller setnavigationbarhidden:isshowhomepage animated:yes];}

7. Change the pop back page

#warning  Write the Viewdidload () method in C //A push to B, B push to C, C pop to d,d and pop to A//  Create a variable copy object, and then replace the /c5>nsmutablearray *navchildmarr = [Self.navigationController.childViewControllers mutablecopy];[ Navchildmarr replaceobjectatindex:1  withobject:d]; // and, of course, finally assign the replaced array back and don't forget [Self.navigationcontroller Setviewcontrollers:navchildmarr Animated:yes];

Modify the pop back page

iOS Uinavigationcontroller navigation bar

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.