We found that when setting Navigationitem's Leftbarbuttonitem or Rightbarbuttonitem, Uibarbuttonitem was initialized with CustomView, No matter how to set customview frame, after adding to the navigation bar and always have a certain amount of space (5pix), how to adjust the spacing freely?
Initializes a Uibarbuttonitem instance negativespacer that controls the spacing, and sets the value of the width property of the Negativespacer, set to 5, so that the button and the screen boundary value are 0, Take the Rightbarbuttonitem case as an example
[Plain] View Plaincopy
- UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];
- /**
- * Set Frame to control button size only
- */
- Btn.frame= cgrectmake (0, 0, 40, 44);
- [Btn addtarget:self Action: @selector (buttonclicked) forcontrolevents:uicontroleventtouchupinside];
- Uibarbuttonitem *btn_right = [[Uibarbuttonitem alloc] initwithcustomview:btn];
- Uibarbuttonitem *negativespacer = [[Uibarbuttonitem alloc]
- Initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace
- Target:nil Action:nil];
- /**
- * Width is negative, equivalent to BTN to the right to move the width of the value of pixels, because the button itself and the boundary spacing is 5pix, so width is set to 5, the spacing is exactly adjusted
- * When the 0;width is positive, the opposite is the equivalent of moving the width to the left by a number of pixels
- */
- Negativespacer.width =-5;
- Self.navigationItem.rightBarButtonItems = [Nsarray arraywithobjects:negativespacer, btn_right, nil];
- [Btn_right release];
Let's look at an example:
UIButton *btnback = [UIButtonbuttonwithtype: Uibuttontypecustom];
Btnback. Frame = CGRectMake (0,0, ata);
Btnback. BackgroundColor = [uicolorclearcolor];
[btnback setimage: [UIImageimagenamed: @ "Bar_back_nomal.png" ]forstate:uicontrolstatenormal];
[btnback setimage: [UIImageimagenamed:@ "Bar_ Back_selected.png "]forstate:uicontrolstatehighlighted];
[btnback setimage: [UIImageimagenamed: @ "bar_back_selected.png"]forstate: uicontrolstateselected];
[btnback addTarget:selfAction:@selector (btnbackclicked:)forcontrolevents:uicontroleventtouchupinside];
[self. Navigationitem setleftbarbuttonitem: [[uibarbuttonitemalloc ]initwithcustomview: Btnback]];
UIButton * Source = [UIButtonbuttonwithtype: Uibuttontypecustom];
source. Frame = CGRectMake (0,0, +);
source. BackgroundColor = [uicolorclearcolor];
[Source setimage: [UIImageimagenamed:@ "Head_ Normal.png "]forstate:uicontrolstatenormal];
[Source setimage: [UIImageimagenamed: @ "head_press.png"]forstate:uicontrolstatehighlighted ];
[Source setimage: [UIImageimagenamed:@ "Head_ Press.png "]forstate:uicontrolstateselected];
[Source addTarget:selfAction: @selector(btnpublicnum:)forcontrolevents: UIControlEventTouchUpInside];
uibarbuttonitem * btn_right = [[uibarbuttonitemalloc] initwithcustomview: source];
uibarbuttonitem * negativespacer = [[uibarbuttonitem Alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target: nil action: nil ];
Negativespacer. width = - 6 ;
self. Navigationitem . Rightbarbuttonitems = [nsarrayarraywithobjects: Negativespacer, Btn_right, Nil ];
OK: The right and left borders are identical
Adjust the spacing between Leftbarbuttonitem and rightbarbuttonitem on the navigation bar and screen boundaries