如何在UINavigationBar 中编辑左侧,右侧UIBarButtonItem的空白空间

我早些时候使用的是iOS 6.1,但是现在我已经转移到了iOS 7.除了其他问题,我还观察到在我的导航栏中,左侧栏button项目的左侧空间和右侧button栏项目的右侧空白区域相当在IOS 7中比在iOS 6中更多。

我需要知道是否有一种方法可以减less导航栏中的左侧,右侧栏button项目的空白空间?

提前致谢。

我也遇到了这个问题。 我也有感觉,在iOS 7有更多的空间。 我发现这大约要多10分。 当我想让LeftBarItemButton从边缘开始时,我通常使用负空格。 这对你也是有用的。

 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -16; // it was -6 in iOS 6 [self.navigationItem setLeftBarButtonItems:@[negativeSpacer, requiredButton /* this will be the button which you actually need */] animated:NO]; 

基于@C_X他的回答我创build了一个类别,它添加和定位基于当前设备的iOS版本的UIBarButtonItem。

 // UINavigationItem+Additions.h @interface UINavigationItem (Additions) - (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem; - (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem; @end // UINavigationItem+Additions.m @implementation UINavigationItem (Additions) - (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { // Add a negative spacer on iOS >= 7.0 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -10; [self setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, leftBarButtonItem, nil]]; } else { // Just set the UIBarButtonItem as you would normally [self setLeftBarButtonItem:leftBarButtonItem]; } } - (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { // Add a negative spacer on iOS >= 7.0 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -10; [self setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, rightBarButtonItem, nil]]; } else { // Just set the UIBarButtonItem as you would normally [self setRightBarButtonItem:rightBarButtonItem]; } } @end 

在你的视图控制器,你现在可以使用[self.navigationItem addLeftBarButtonItem:leftBarButtonItem];[self.navigationItem addRightBarButtonItem:rightBarButtonItem];

我也尝试了-alignmentRectInsets UIButton并重写-alignmentRectInsets但这给了我之间的过渡视图的问题。

对于Swift 2.0,这是我的解决scheme,以获得以下效果…

(实际值可能会有所不同,具体取决于您的情况)

在这里输入图像说明

 let captureButton = UIButton() captureButton.setTitle("CAPTURE DETAILS", forState: .Normal) captureButton.frame = CGRectMake(0, 0, 200, 95) captureButton.addTarget(self, action: Selector("showCaptureDetailsForm:"), forControlEvents: .TouchUpInside) // *** See update below for Swift 2.2 syntax captureButton.setBackgroundImage(UIImage(named: "blueTopRight"), forState: .Normal) let rightBarButton = UIBarButtonItem() rightBarButton.customView = captureButton let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil) negativeSpacer.width = -25; self.navigationItem.setRightBarButtonItems([negativeSpacer, rightBarButton ], animated: false) 

Swift 2.2更新:

对于Swift 2.2, action: Selector方法已经改变,应该如下input

captureButton.addTarget(self, action: #selector(YourViewController.showCaptureDetailsForm(_:)), forControlEvents: .TouchUpInside)

在smek的领导下,我做了一个类别,但是对它进行了修改,以提供向后兼容性而不是转发。 我设置了一切工作,我想如何在iOS 7中,然后如果用户正在运行的东西更低,我开始与东西混杂。

 @interface UINavigationItem (BarButtonItemSpacingSupport) - (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem; - (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem; @end @implementation UINavigationItem (BarButtonItemSpacingSupport) - (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem { if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { // Add a spacer on when running lower than iOS 7.0 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = 10; [self setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, leftBarButtonItem, nil]]; } else { // Just set the UIBarButtonItem as you would normally [self setLeftBarButtonItem:leftBarButtonItem]; } } - (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem { if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { // Add a spacer on when running lower than iOS 7.0 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = 10; [self setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, rightBarButtonItem, nil]]; } else { // Just set the UIBarButtonItem as you would normally [self setRightBarButtonItem:rightBarButtonItem]; } } @end 

然后为了得到这个全局,我有一个瘦UIViewController子类,所有我的视图控制器inheritance。

 @interface INFViewController : UIViewController @end @implementation INFViewController - (void)viewDidLoad { [super viewDidLoad]; if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { [self setupNavBarForPreIOS7Support]; } } - (void)setupNavBarForPreIOS7Support { if (self.navigationController) { UINavigationItem *navigationItem = self.navigationItem; UIBarButtonItem *leftItem = navigationItem.leftBarButtonItem; UIBarButtonItem *rightItem = navigationItem.rightBarButtonItem; if (leftItem) { [navigationItem addLeftBarButtonItem:leftItem]; } if (rightItem) { [navigationItem addRightBarButtonItem:rightItem]; } } } @end 

我意识到我正在检查操作系统版本两次(一次在INFViewController ,再次在类别中),我把它留在类别incase我想在项目中的任何地方一次性使用它。

为了解决这个错误,你必须UIButton这样你才能覆盖alignmentRectInsets 。 从我的testing中,您需要根据button的位置,返回一个带正向右偏移量或正向左偏移量的UIEdgeInsets 。 这些数字对我来说是毫无意义的(根据常识,至less其中一个数据应该是负数),但这是实际工作的结果:

 - (UIEdgeInsets)alignmentRectInsets { UIEdgeInsets insets; if (IF_ITS_A_LEFT_BUTTON) { insets = UIEdgeInsetsMake(0, 9.0f, 0, 0); } else { // IF_ITS_A_RIGHT_BUTTON insets = UIEdgeInsetsMake(0, 0, 0, 9.0f); } return insets; } 

特别感谢@zevbuild议我尝试调整alignmentRectInsets

为了迅速,你可以做到这一点

 var negativeSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil) negativeSpace.width = -17.0 self.navigationItem.rightBarButtonItems = [negativeSpace, requiredButton /* this will be the button which you actually need */] 

Swift 3:

 let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil) negativeSpacer.width = -10 self.navigationItem.leftBarButtonItems = [negativeSpacer, yourBarButtonItem] 

这是我对swift 3.0的解决scheme:

rightBtn.imageInsets = UIEdgeInsets(top:0,left:-13.0,bottom:0,right:13.0)self.navigationItem.rightBarButtonItem = rightBtn

我相信你需要使用UIButton子类的自定义button,并在你的子类,重写-alignmentRectInsets 。 我忘了你是否需要一个正面或负面的价值适当的边缘让它正确地转移,但如果一个不工作,尝试另一个。

Swift 3.1

给左栏button项目负空间:

  let backButton = UIButton.init(type: .custom) backButton.frame = CGRect.init(x: 0, y: 0, width: 40, height: 40) // negative space backButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: -44.0, bottom: 0, right: 0) backButton.setImage(Ionicons.iosArrowBack.image(30, color: UIColor(hex: 0xFD6250)), for: .normal) backButton.addTarget(self, action: #selector(InviteVC.goBack), for: .touchUpInside) // set back button self.navigationItem.leftBarButtonIteUIBarButtonItem.init(customView: backButton) 

很好的决定,非常感谢! 我只需要在导航标题的左侧添加两个元素。 这是我的解决scheme:

  // Settings Button // This trick correct spacing between two left buttons UIBarButtonItem *settingsButtonItem = [[UIBarButtonItem alloc] init]; UIView *settingsButtonItemView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 22.0, 22.0)]; settingsButtonItem.customView = settingsButtonItemView; UIButton *settingsButton = [UIButton buttonWithType:UIButtonTypeSystem]; settingsButton.frame = settingsButtonItemView.frame; [settingsButton setImage:[UIImage imageNamed:@"settings"] forState:UIControlStateNormal]; settingsButton.tintColor = [[[[UIApplication sharedApplication] delegate] window] tintColor]; [settingsButton addTarget:self action:@selector(showSettings:) forControlEvents:UIControlEventTouchDown]; [settingsButtonItemView addSubview:settingsButton]; // Star Button UIBarButtonItem *starButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star_gray"] style:UIBarButtonItemStyleBordered target:self action:@selector(showStarred)]; starButtonItem.width = 22.0; NSLog(@"%f", starButtonItem.width); // Negative Spacer // It shifts star button to the left UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -10.0; NSArray *leftNavigtaionBarButtonItems = @[negativeSpacer, starButtonItem, settingsButtonItem]; [self.navigationItem setLeftBarButtonItems:leftNavigtaionBarButtonItems];