如何调整UIToolBar的左右填充

我创build了一个带有代码的UIToolbar,另一个带有界面生成器。 但是,发现两个工具栏有不同的左右填充,如下所示:

从界面生成器:

在这里输入图像说明

从代码:

在这里输入图像说明

UIImage *buttonImage = [[UIImage imageNamed:@"button.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]; UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom]; [btnTest setBackgroundImage:buttonImage forState:UIControlStateNormal]; [btnTest setTitle:@"Back" forState:UIControlStateNormal]; [btnTest.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; [btnTest setBackgroundImage:[imgToolbarButton stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal]; [btnTest addTarget:self action:@selector(clearDateEdit:) forControlEvents:UIControlEventTouchUpInside]; btnTest.frame = CGRectMake(0.0, 0.0, 50, 30); UIBarButtonItem *btnTestItem = [[UIBarButtonItem alloc] initWithCustomView:btnTest]; [self.toolbar setItems:[NSArray arrayWithObjects:btnTestItem,nil]]; [btnTestItem release]; 

我的问题是如何通过代码调整左侧和右侧的UIToolbar的填充?

更新

我发现这个alignment问题只发生在用UIButton的customView的UIBarButtonItem,UIBarButtonItem的alignment很好。 任何想法是什么原因或解决这个问题。

我现在唯一能想到的解决scheme是手动设置框架。

我有同样的问题,有一个巧妙的技巧,你可以做一个UIBarButtonSystemItemFixedSpace,在第一个button之前和最后一个button之后添加一个负宽度,并将button移动到边缘。

例如,要删除右边距,请将以下FixedSpace栏项目添加为最后一个项目:

 UIBarButtonItem *negativeSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSeparator.width = -12; 

左边和右边的边距是12px。

iOS7更新 – iPhone上的边距为16px,iPad上的边距为20px!

使用这个button

 UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

我遇到了同样的问题。 最后,我通过inheritanceUIToolbar并覆盖布局子视图方法来解决这个问题。

 - (void)layoutSubviews { [super layoutSubviews]; if (leftItem_ && leftItem_.customView && [leftItem_.customView isKindOfClass:[UIButton class]]) { CGRect newFrame = leftItem_.customView.frame; newFrame.origin.x = 0; // reset the original point x to 0; default is 12, wired number leftItem_.customView.frame = newFrame; } if (rightItem_ && rightItem_.customView && [rightItem_.customView isKindOfClass:[UIButton class]]) { CGRect newFrame = rightItem_.customView.frame; newFrame.origin.x = self.frame.size.width - CGRectGetWidth(newFrame); rightItem_.customView.frame = newFrame; } } 

你可以改变工具栏的偏移量和宽度,如果你想使用customview(initWithCustomView)

 [myToolBar setFrame:CGRectMake(-10, 0, [UIScreen mainScreen].bounds.size.width+10, 44)]; 

当试图将工具栏作为UIButton的子视图居中时,我偶然发现了这一点。 由于工具栏需要宽度很小,所以最好的解决scheme是在每一边都有一个灵活的button:

 UIBarButtonItem flexibleButtonItemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem centeredButtonItem = ... UIBarButtonItem flexibleButtonItemRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIToolbar toolbar = ... toolbar.items = @[flexibleButtonItemLeft, centeredButtonItem, flexibleButtonItemRight]; 

最后,为UIBarButtonItem自定义背景图像,并容纳alignment,我已经放弃了UIBarButtonItem和手动添加UIButton。

 UIImage *buttonImage = [[UIImage imageNamed:@"button.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]; UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom]; [btnTest setBackgroundImage:buttonImage forState:UIControlStateNormal]; [btnTest setTitle:@"Back" forState:UIControlStateNormal]; [btnTest.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; [btnTest setBackgroundImage:[imgToolbarButton stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal]; [btnTest addTarget:self action:@selector(clearDateEdit:) forControlEvents:UIControlEventTouchUpInside]; btnTest.frame = CGRectMake(0.0, 0.0, 50, 30); [self.toolbar addSubview:btnTest]; [btnTestItem release];