The role of the iOS development navigation bar control

Source: Internet
Author: User

One, in the iOS development process for some of the navigation bar needs to customize the view, sometimes need to use the system comes with the processing, some times need to customize some views and add the view, this time mainly their location is slightly different, the following simple write down the demo;

1, with the navigation bar system comes with the view processing:

1     //1 picture in the middle2Uiimageview *imagebarview = [[Uiimageview alloc] Initwithframe:cgrectmake (Kscreenwidth/2. F- +. F, -. F, the, -)];3Imagebarview.image = [UIImage imagenamed:@"Middle View name"];4Self.navigationItem.titleView =Imagebarview;5     6     7     //2 category buttons to the right of the navigation bar8UIButton *buttonrightitem =[UIButton Buttonwithtype:uibuttontypecustom];9Buttonrightitem.frame = CGRectMake (0. F,0. F, -. F, -. f);Ten[Buttonrightitem Settitle:@"category"Forstate:uicontrolstatenormal]; One[Buttonrightitem settitlecolor:[uicolor colorwithred:0.310Green0.598Blue0.156Alpha1.000] forstate:uicontrolstatenormal]; AButtonRightItem.titleLabel.font = [Uifont systemfontofsize: -]; - [Buttonrightitem addtarget:self Action: @selector (buttonrightaction:) forControlEvents: UIControlEventTouchUpInside]; -Self.navigationItem.rightBarButtonItem =[[Uibarbuttonitem alloc] initwithcustomview:buttonrightitem]; the      -     //3 App button on the left side of the navigation bar -UIButton *buttonleftitem =[UIButton Buttonwithtype:uibuttontypecustom]; -Buttonleftitem.frame = CGRectMake (0. F,0. F, -. F, -. f); +[Buttonleftitem setimage:[uiimage imagenamed:@"left View"] forstate:uicontrolstatenormal]; -Buttonleftitem.imageedgeinsets = Uiedgeinsetsmake (0. F,- -. F,0. F,0. f);//used to adjust relative position + [Buttonleftitem addtarget:self Action: @selector (buttonleftaction:) forControlEvents: UIControlEventTouchUpInside]; ASelf.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithcustomview:buttonleftitem];

In fact, the above key is:

Self.navigationItem.titleView = Imagebarview;

Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] initwithcustomview:buttonrightitem];
Self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithcustomview:buttonleftitem];

The main is the above three words, this advantage is left, middle, right three buttons of the response event area is relatively large, relatively easy to control, generally recommended to use the system to operate;

The disadvantage is that each zone can only have one control, the following with a custom method to put two or more controls in the same area;

2. Customize the view on the navigation bar;

1Rightbutton =[UIButton Buttonwithtype:uibuttontypecustom];2Rightbutton.backgroundcolor =[Uicolor Blackcolor];3[Rightbutton Settitle:@"Share"Forstate:uicontrolstatenormal];4 [Rightbutton addtarget:self Action: @selector (shareaction:) forcontrolevents:uicontroleventtouchupinside];5Rightbutton.frame = CGRectMake (Self.view.width- -. F,0. F, -. F, -. f);6 [Self.navigationController.navigationBar Addsubview:rightbutton];7     8Righttwobutton =[UIButton Buttonwithtype:uibuttontypecustom];9Righttwobutton.backgroundcolor =[Uicolor Bluecolor];Ten[Righttwobutton Settitle:@"Collection"Forstate:uicontrolstatenormal]; One [Righttwobutton addtarget:self Action: @selector (storeaction:) forcontrolevents:uicontroleventtouchupinside];  ARighttwobutton.frame = CGRectMake (Self.view.width- -. F,0. F, -. F, -. f); -[Self.navigationController.navigationBar Addsubview:righttwobutton];

The above method can put the view very wayward in the navigation bar anywhere, because the use of Self.navigationController.navigationBar to add, this is the entire navigation bar, but this frame needs to be carefully adjusted, Otherwise, the layout will be difficult to see, in addition, when the view to appear or disappear, you need to handle the button;

1- (void) Viewwilldisappear: (BOOL) Animated2 {3 [Super viewwilldisappear:animated];4     5[UIView animatewithduration:0.2animations:^{6         7Rightbutton.alpha =0;8Righttwobutton.alpha =0;9 Ten     }]; One } A  -- (void) Viewwillappear: (BOOL) animated{ -      the [Super viewwillappear:animated]; -[UIView animatewithduration:0.2animations:^{ -          -Rightbutton.alpha =1; +Righttwobutton.alpha =1; -          +     }]; A  at}

The above is the navigation bar view on the processing, may be relatively simple, but for beginners is also more important;

Tip: Remove the shading method below the navigation bar:

Add this sentence, you can remove the shadowImage below the navigation bar, you can normally display self.navigationController.navigationBar.shadowImage = [[UIImage alloc] Init   ]; Self.navigationController.navigationBar.translucent = NO;

The role of the iOS development navigation bar control

Related Article

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.