Uibarbuttonitem distance adjustment for IOS Uitoolbar and Uinavigationbar

Source: Internet
Author: User

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

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.