如何在 UINavigationBar [iOS 7] 中编辑左、右 UIBarButtonItem 的空白区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18914812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]
提问by Salman Zaidi
I was using iOS 6.1 earlier, but now I have moved to iOS 7. Along with other problems, I have observed that in my navigation bar, the left space of left bar button item and right empty space of the right button bar item are quite more in IOS 7 than in iOS 6.
我之前使用的是iOS 6.1,现在我已经转移到iOS 7。除了其他问题之外,我观察到在我的导航栏中,左侧栏按钮项的左侧空间和右侧按钮栏项的右侧空白空间相当在 IOS 7 中比在 iOS 6 中更多。
I need to know is there a way I can reduce empty spaces of left, right bar button items in navigation bar??
我需要知道有没有办法可以减少导航栏中左右栏按钮项的空白空间?
Thanks in advance.
提前致谢。
回答by Adnan Aftab
I was also facing this problem. I also have feelings that in iOS 7 there is more space. And I figured out that this is about 10 points more. I usually use negative spaces when I want for LeftBarItemButton
to start from the edge. This can be useful for you as well.
我也面临这个问题。我也有感觉,在 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];
回答by smek
Based on @C_X his answer I've created a category which adds and positions the UIBarButtonItem based on the iOS version of the current device.
基于@C_X 他的回答,我创建了一个类别,该类别根据当前设备的 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
In your view controller you can now use [self.navigationItem addLeftBarButtonItem:leftBarButtonItem];
and [self.navigationItem addRightBarButtonItem:rightBarButtonItem];
在您的视图控制器中,您现在可以使用[self.navigationItem addLeftBarButtonItem:leftBarButtonItem];
和[self.navigationItem addRightBarButtonItem:rightBarButtonItem];
I've also tried subclassing UIButton
and override -alignmentRectInsets
but this gave me problems with transitions between views.
我也尝试过子类化UIButton
和覆盖,-alignmentRectInsets
但这给我带来了视图之间转换的问题。
回答by Simon
For Swift 2.0, this was my solution to get the following effect...
对于Swift 2.0,这是我获得以下效果的解决方案......
(the actual values may be different, depending on your situation)
(实际值可能会有所不同,具体取决于您的情况)
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 UPDATE:
斯威夫特 2.2 更新:
For Swift 2.2, the action: Selector
method has changed, and should be typed as follows
对于 Swift 2.2,action: Selector
方法已更改,应键入如下
captureButton.addTarget(self, action: #selector(YourViewController.showCaptureDetailsForm(_:)), forControlEvents: .TouchUpInside)
captureButton.addTarget(self, action: #selector(YourViewController.showCaptureDetailsForm(_:)), forControlEvents: .TouchUpInside)
回答by tuandapen
This is my solution for Swift 3.0
:
这是我的解决方案Swift 3.0
:
rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -13.0, bottom: 0, right: 13.0) self.navigationItem.rightBarButtonItem = rightBtn
回答by jaredsinclair
In order to fix this bug, you must subclass UIButton
so that you can override alignmentRectInsets
. From my testing, you'll need to return a UIEdgeInsets
with either a positive right offset or a positive left offset, depending on the button position. These numbers make no sense to me (at least one of them should be negative, according to common sense), but this is what actually works:
为了修复这个错误,你必须子类化UIButton
以便你可以覆盖alignmentRectInsets
. 根据我的测试,您需要UIEdgeInsets
根据按钮位置返回带有正右偏移量或正左偏移量的 a 。这些数字对我来说毫无意义(根据常识,其中至少有一个应该是负数),但这才是真正有效的方法:
- (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;
}
Special thanks to @zev for suggesting I try adjusting alignmentRectInsets
.
特别感谢@zev 建议我尝试调整alignmentRectInsets
.
回答by reza pahlevi
for swift you can do this
为了快速,你可以这样做
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 */]
回答by Chris Wagner
Following smek's leadI made a category but modified it to provide backwards compatibility rather than forwards. I setup everything to work how I want it in iOS 7 and then if the user is running something lower I start mucking with things.
在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
And then to get this globally, I have a thin UIViewController
subclass that all of my view controllers inherit from.
然后为了全局获取它,我有一个瘦子UIViewController
类,我的所有视图控制器都继承自它。
@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
I realize that I am checking the OS version twice (once in INFViewController
and again in the category), I left it in the category incase I want to use this as a one-off anywhere in the project.
我意识到我正在检查操作系统版本两次(一次INFViewController
又一次在类别中),我将它留在类别中,以防我想在项目的任何地方一次性使用它。
回答by vinny
As of iOS 11 wont accept negative space width, in order to align the bar button items to the margin, I have used the below code.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
for view in (self.navigationController?.navigationBar.subviews)! {
view.layoutMargins = UIEdgeInsets.zero
}
}
回答by Dvole
Swift 3:
斯威夫特 3:
let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -10
self.navigationItem.leftBarButtonItems = [negativeSpacer, yourBarButtonItem]
回答by rp2701
Swift 3.1
斯威夫特 3.1
To give left bar button item negative space:
给左栏按钮项目负空间:
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)