【发布时间】:2015-01-21 20:00:25
【问题描述】:
我正在努力解决 iOS7+ iPhone 应用程序中 UIToolbar 的奇怪行为。
这是我创建它的方法:
//toolbar
self.bottomToolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.bounds.size.height-44.0, self.view.bounds.size.width, 44.0)];
self.bottomToolbar.autoresizingMask=(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth);
[MKUIHelper toolbar:self.bottomToolbar setBarStyle:MKBarStyleWhite];
self.bottomToolbar.items=@[];
[self.view addSubview:self.bottomToolbar];
当我需要更新它时,我会打电话(实际上只更新其中一项,但这并不重要):
-(void)updateToolbar
{
UIBarButtonItem *doneBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonDidPress:)];
doneBarButtonItem.tintColor=MK_Color_Green;
UIBarButtonItem *clearBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"clear" style:UIBarButtonItemStylePlain target:self action:@selector(clearButtonDidPress:)];
clearBarButtonItem.tintColor=MK_Color_Pink;
UIBarButtonItem *spaceBetweenBackAndQuantity=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *spaceBetweenQuantityAndClear=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *quantityItem=[self quantityBarButtonItem];
self.bottomToolbar.items=[NSArray arrayWithObjects:doneBarButtonItem, spaceBetweenBackAndQuantity, quantityItem, spaceBetweenQuantityAndClear, clearBarButtonItem, nil];
[self.rootViewController reloadToolbar];
}
使用initWithTitle:.. 创建的UIBarButtonItem 对象都以某种方式具有负的左右填充。可以通过在 items 数组的两侧添加额外的固定空间类型 UIBarButtonItems 来大致修复它。换句话说:用固定空间类型 UIBarButtonItems 补偿奇怪的初始左右填充。
请帮帮我。是什么导致了这种奇怪的效果?我怎样才能摆脱这种奇怪的填充?
它是这样的:
【问题讨论】:
-
你能截图看看这个问题的样子吗?
-
用工具栏图片编辑了我的问题
-
我不确定
autoresizingMask是否与您的问题有关,但请尝试删除该行,看看会发生什么。另外,你在使用自动布局吗? -
查看调试可能会帮助您了解实际发生的情况。
-
不使用自动布局。
标签: ios objective-c iphone uikit uitoolbar