在使用LeftBarItems和RightBarItems时,我发现uibarbuttonite填充/间距过大(见下图).UIBarButtonims上使用的图标不包含额外的填充.我想知道是什么原因造成的?

在此处输入图像描述

推荐答案

我用它来删除第一项之前的空间.

然而,它不能在系统项(如UIBarButtonSystemItemAdd)之间工作,只适用于有图像的uibarbuttonite.

@interface UIBarButtonItem (NegativeSpacer)
+(UIBarButtonItem*)negativeSpacerWithWidth:(NSInteger)width;
@end
@implementation UIBarButtonItem (NegativeSpacer)
+(UIBarButtonItem*)negativeSpacerWithWidth:(NSInteger)width {
    UIBarButtonItem *item = [[UIBarButtonItem alloc]
                             initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                             target:nil
                             action:nil];
    item.width = (width >= 0 ? -width : width);
    return item;
}
@end

这样使用:


UIBarButtonItem *item0 = [UIBarButtonItem negativeSpacerWithWidth:13];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sidebar.png"]
                                                          style:UIBarButtonItemStylePlain
                                                         target:vc
                                                         action:@selector(sideMenuAction:)];
NSArray* items = @[item0, item1];
[vc.navigationItem setLeftBarButtonItems:items animated:NO];
[vc.navigationItem setLeftItemsSupplementBackButton:YES];

Objective-c相关问答推荐

核心数据独特属性

Xcode 是否支持区域?

UISearchDisplayController 没有结果tableView?

从 NSError 返回 NSString

如何更改图像 tintColor

切换隐私设置将杀死应用程序

有没有一种简单的方法可以将 ISO8601 时间戳转换为格式化的 NSDate?

iOS框架改变一个属性(例如宽度)

UIWebView 不会zoom 内容以适应

我如何使用 initWithCoder 方法?

如何计算多边形的圆角?

UIFont 的磅值到底是多少?

强制iphone应用程序以编程方式重新启动?

AdMob 因 [GADObjectPrivate changeState:] 崩溃:无法识别的 Select 器

无法加载 xcode 项目,因为它已从另一个项目或工作区打开

如何在 ios 8 的 alertview 中添加文本输入?

Objective-C:前向类声明

在objective c中以自定义格式获取当前时间

字符串常量和字符串文字有什么区别?

Xcode没有编译任何项目? 'clang 失败,退出代码 255'