【问题标题】:UIToolbar left and right paddings are initially negativeUIToolbar 左右填充最初为负数
【发布时间】: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


【解决方案1】:

如果您将 barButtons 的宽度设置为正确的宽度,您也许可以解决这个问题。

- (UIBarButtonItem *)barButtonWithTitle:(NSString *)title action:(SEL)action {

    UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action] autorelease];


    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    [button setTitle:title forState: UIControlStateNormal];
    [button sizeToFit];
    CGSize size = button.bounds.size;

    [barButtonItem setWidth:size.width];

    return barButtonItem;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多