skå*_*fan 4 iphone cocoa-touch uitoolbar ios4
我正在创建一个UIToolbar,只有一个UILabel放在它上面.标签内容是动态的,可以改变,我想把它放在UIToolbar上.
有谁知道UIToolbar左右两侧的确切边距大小?
我正在创建[[UIBarButtonItem alloc] initWithCustomView:myLabelContainer],我正在尝试确保myLabelContainer占用整个工具栏空间,减去强制边距.
现在我猜测每边的边距大约是12像素,所以框架宽度为296的UIView应该填满整个UIToolbar?
这些信息是否随处可用?
我认为这些信息不可用.但通过一些测试应该很容易.
如果你所需要的仅仅是整个工具栏上,一个UILabel,我会建议只是将它添加到UIToolbar,这是一个UIView子类.你不需要你的所有UIBarButtonItem 功能....只是我认为的背景.
然后设置UILabel.frame(使用你想要的边距)+ addSubview:+ a UITextAlignmentCenter应该完成这项工作!UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight如果你有管理设备方向,也可以使用autoresizingMask ...
编辑1:
由于对该提案存在一些疑问,我做了一个快速测试:
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)] autorelease];
label.text = @"Hello world";
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.backgroundColor = RGBACOLOR(255, 0, 0, .3f);
[self.toolbar addSubview:label];
Run Code Online (Sandbox Code Playgroud)
结果如下:

编辑2:
现在我已经开始使用Xcode并编写了一些代码,这很容易找出你的主要问题.改变前面的代码:
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
... same
[self.toolbar setItems:
[NSArray arrayWithObject:
[[[UIBarButtonItem alloc] initWithCustomView:label] autorelease]]];
Run Code Online (Sandbox Code Playgroud)
带来:

因此,正如您猜测的那样,左边距为12px,但自定义视图看起来不适合调整大小,因此,在这种情况下没有正确的边距...除非您相应地调整视图大小.
编辑3:
除非你的UILabel需要一个背景,否则这就是我可能会做的事情(这UIBarButtonSystemItemFlexibleSpace毕竟是什么......):
UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = @"Hello world";
... same
[label sizeToFit];
[self.toolbar setItems:
[NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil] autorelease],
[[[UIBarButtonItem alloc] initWithCustomView:label] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil] autorelease],
nil]];
Run Code Online (Sandbox Code Playgroud)
结果 :

编辑4:
作为一个方面,在更多工作之后UIToolbar,12px是在按钮之前添加的默认空间.我刚刚发现了一个有趣的,间隔符正在使用负值!.IE如果你想要两个按钮之间的2px空间,只需添加一个-10px UIBarButtonSystemItemFixedSpace...方便!
| 归档时间: |
|
| 查看次数: |
4122 次 |
| 最近记录: |