During iOS development, multiple Uibarbutton distances added to Uitoolbar and Uinavigationbar are usually fixed and cannot be adjusted.
For example, when adding more than two Uibarbutton to the right of the Uinavigationbar, two Uibarbutton plus the middle gap occupy a larger portion of the space,
As a result, developers often use Uisegmentedcontrol to replace two button when developing iphone applications.
In fact, if you use some of the characteristics of Uitoolbar can achieve the Uibarbutton spacing adjustment, the following methods:
1. Define a Uitoolbar to store two button
2. Add a button with a uibarbuttonsystemitemflexiblespace type between the two button
3. Adjust the width of the Uitoolbar, then you can directly adjust the width of the two button blank
4. Add Uitoolbar to Navigationbar
The 4th step, using the early iOS4 time to add multiple Uibarbutton to Uitoolbar and Uinavigationbar methods can
The implementation code is as follows
The code is as follows |
Copy Code |
Button1 Uibarbuttonitem *button1 = [[Uibarbuttonitem alloc] initwithtitle:@ "Button1" Style:uibarbuttonitemstylebordered Target:nil Action:nil]; Button1 Uibarbuttonitem *button2 = [[Uibarbuttonitem alloc] initwithtitle:@ "Button2" Style:uibarbuttonitemstylebordered Target:nil Action:nil]; Uibarbuttonitem *space = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action:nil]; Initialize Toolbar Uitoolbar *righttoolbar = [[Uitoolbar alloc]init]; Righttoolbar.items = [Nsarray arraywithobjects:button1,space,button2,nil];
Align the button's tint color with the navigation bar Righttoolbar.tintcolor = Self.navigationController.navigationBar.tintColor; Adjust width to narrow the button spacing Righttoolbar.frame = CGRectMake (220, 0, 101, 44);
Remove background to add to Uitoolbar or Uinavigationbar Righttoolbar.backgroundcolor = [Uicolor Clearcolor]; For (UIView *view in [Righttoolbar Subviews]) { if ([View Iskindofclass:[uiimageview class]]) { [View Removefromsuperview]; } } Add to Navigationbar [Self.navigationController.visibleViewController |