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