IOS8 Custom Navigationbar and Uibarbuttonitem margin issues

Source: Internet
Author: User

One. Custom Navigationbar

- (void) initnavigationbar{[Self.navigationcontroller Setnavigationbarhidden:yes]; Uinavigationbar*bar = [[Uinavigationbar alloc] Initwithframe:cgrectmake (0,0, the, +)]; [Bar setbackgroundimage:[uiimage imagenamed:@"Zhuche_bar2.png"] Forbarmetrics:uibarmetricsdefault]; Uinavigationitem*item =[[Uinavigationitem alloc] initwithtitle:nil]; UIButton*left =[UIButton Buttonwithtype:uibuttontypecustom]; [Left Setframe:cgrectmake (0,2, -, -)]; [Left Setimage:[uiimage imagenamed:@"Zhuche_back.png"] Forstate:uicontrolstatenormal];    [Left Addtarget:self action: @selector (back) forcontrolevents:uicontroleventtouchupinside]; Uibarbuttonitem*leftbutton =[[Uibarbuttonitem alloc] initwithcustomview:left];    [Item Setleftbarbuttonitem:leftbutton];    [Bar Pushnavigationitem:item Animated:no]; [Self.view Addsubview:bar];}- (void) back{[Self.navigationcontroller popviewcontrolleranimated:yes];}

Although you can solve the problem of custom Navigationbar, the left and right buttons can be replaced, but if placed in Uitableviewcontroller, the custom Navigationbar will slide with the ScrollView slide, Does not always stay at the top of the screen as the system comes with Navigationbar. Then we can have a second solution, modify the original Navigationbar, in fact, is to change the Leftbarbuttonitem or Rightbarbuttonitem in the navigationbar above position: Modify The margin of the Uibarbuttonitem .

Two. to modify the margins of a uibarbuttonitem

@interfaceUinavigationitem (margin)@end@implementationUinavigationitem (margin)#if__iphone_os_version_max_allowed > __iphone_6_1-(void) Setleftbarbuttonitem: (Uibarbuttonitem *) _leftbarbuttonitem{if([[[[Uidevice Currentdevice] systemversion] Floatvalue] >=7.0) {Uibarbuttonitem*negativeseperator =[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target:nil Action:nil]; Negativeseperator.width= - -;//to modify the distance to the boundary here, please test yourself                if(_leftbarbuttonitem) {[Self setleftbarbuttonitems:@[negativeseperator, _leftbarbuttonitem]]; }        Else{[self setleftbarbuttonitems:@[negativeseperator]]; }                   }    Else{[self setleftbarbuttonitem:_leftbarbuttonitem animated:no]; }}- (void) Setrightbarbuttonitem: (Uibarbuttonitem *) _rightbarbuttonitem{if([[[[Uidevice Currentdevice] systemversion] Floatvalue] >=7.0) {Uibarbuttonitem*negativeseperator =[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target:nil Action:nil]; Negativeseperator.width= - A;//to modify the distance to the boundary here, please test yourself                if(_rightbarbuttonitem) {[Self setrightbarbuttonitems:@[negativeseperator, _rightbarbuttonitem]]; }        Else{[self setrightbarbuttonitems:@[negativeseperator]]; }            }    Else{[self setrightbarbuttonitem:_rightbarbuttonitem animated:no]; }}#endif@end

The above code can be placed in the. m file of the main view controller,@implementation the top of the Viewcontroller .

Reference: http://blog.csdn.net/reylen/article/details/16338781;

http://blog.163.com/lhl_soft/blog/static/201750004201292043750125/;


IOS8 Custom Navigationbar and Uibarbuttonitem margin issues

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.