Adjust the spacing between Leftbarbuttonitem and rightbarbuttonitem on the navigation bar and screen boundaries

Source: Internet
Author: User

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



  1. UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];
  2. /**
  3. * Set Frame to control button size only
  4. */
  5. Btn.frame= cgrectmake (0, 0, 40, 44);
  6. [Btn addtarget:self Action: @selector (buttonclicked) forcontrolevents:uicontroleventtouchupinside];
  7. Uibarbuttonitem *btn_right = [[Uibarbuttonitem alloc] initwithcustomview:btn];
  8. Uibarbuttonitem *negativespacer = [[Uibarbuttonitem alloc]
  9. Initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace
  10. Target:nil Action:nil];
  11. /**
  12. * 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
  13. * When the 0;width is positive, the opposite is the equivalent of moving the width to the left by a number of pixels
  14. */
  15. Negativespacer.width =-5;
  16. Self.navigationItem.rightBarButtonItems = [Nsarray arraywithobjects:negativespacer, btn_right, nil];
  17. [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

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.