如何调整UIToolBar左右填充 [英] How to adjust UIToolBar left and right padding

查看:84
本文介绍了如何调整UIToolBar左右填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create one UIToolbar with code and another with interface builder. But, found out the two toolbar having different left and right padding which shown below:

来自Interface Builder:

From Interface Builder:

来自代码:

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的左右填充?

My question is how can I adjust the left and right padding of UIToolbar by code?

更新

我发现这个对齐问题只发生在UIBarButtonItem上,带有UIButton的customView,对齐UIBarButtonItem很好用。不知道是什么导致这个或解决这个问题。

I discovered this alignment issue only happen to UIBarButtonItem with customView of UIButton, the alignment is fine with UIBarButtonItem. Any idea what cause this or to resolve this.

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

The only solution I can think of right now is to manually set the frame.

推荐答案

我遇到了同样的问题,你可以用UIBarButtonSystemItemFixedSpace做一个巧妙的技巧,在第一个按钮之前和最后一个按钮之后加一个负宽度按钮,它会将按钮移动到边缘。

I had the same issue, and there's a neat trick you can do with a UIBarButtonSystemItemFixedSpace, add one of these with a negative width before your first button and after your last button and it will move the button to the edge.

例如,要摆脱右边距,请添加以下FixedSpace条形项作为最后一项:

For example, to get rid of the right hand margin add the following FixedSpace bar item as the last item:

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

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

The margins on the left and right are 12px.

iOS7更新 - iPhone的利润率为16px,iPad的利润率为20px!

Update for iOS7 - margins are 16px on iPhone and 20px on iPad!

这篇关于如何调整UIToolBar左右填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆