如何调整rightBarButtonItems中两个UIBarButtonItem之间的空间 [英] How to adjust space between two UIBarButtonItem in rightBarButtonItems
问题描述
我使用以下代码将两个按钮添加到self.navigationItem.rightBarButtonItems,我认为在iOS7中,两个按钮之间的空间太宽,有没有办法减少这两个按钮之间的空间?
I am using the following codes to add two button to self.navigationItem.rightBarButtonItems, and I think in iOS7, the space between two buttons are too wide, is there a way to decrease the space between these two buttons?
UIBarButtonItem *saveStyleButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"save.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(saveStyle)];
UIBarButtonItem *shareStyleButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareStyle)];
NSArray *arr= [[NSArray alloc] initWithObjects:shareStyleButton,saveStyleButton,nil];
self.navigationItem.rightBarButtonItems=arr;
欣赏任何提示或想法。
Appreciate any hint or idea.
推荐答案
于2015年7月更新
更好的方法是使用故事板(在Xcode 6.4中测试)。首先,添加一个UINavigationItem;其次,添加一个条形按钮项;第三,将视图添加到刚刚在步骤2中创建的条形按钮项;第四,在你刚刚拖入的视图中添加任意数量的按钮;最后,用鼠标和约束调整空间。
A better way to do this is to use storyboard (tested in Xcode 6.4). First, add a UINavigationItem; secondly, add a Bar Button Item; thirdly, add a view to the Bar Button Item you just created in step 2; fourthly, add as many buttons as you wish into that view you just dragged in; lastly, adjust the space with your mouse and constraints.
相关问题
在iOS 5中使用Storyboard时,无法为UINavigationItem分配多个按钮
旧答案(仅适用于小插图)
使用imageInsets属性:
Use imageInsets property:
leftButton.imageInsets = UIEdgeInsetsMake(0.0, 0.0, 0, -15);
rightButton.imageInsets = UIEdgeInsetsMake(0.0, -15, 0, 0);
三个或更多按钮,中间的按钮得到两个插图:
for three or more buttons, the middle one(s) get both insets:
leftButton.imageInsets = UIEdgeInsetsMake(0.0, 0.0, 0, -15);
middleButton.imageInsets = UIEdgeInsetsMake(0.0, -15, 0, -15);
rightButton.imageInsets = UIEdgeInsetsMake(0.0, -15, 0, 0);
对于右侧按钮,请注意:项目数组中的FIRST按钮是正确的:
For the right side buttons, be careful: the FIRST button in the item array is the RIGHT one:
rightButton.imageInsets = UIEdgeInsetsMake(0.0, -15, 0, 0);
middleButton.imageInsets = UIEdgeInsetsMake(0.0, -15, 0, -15);
leftButton.imageInsets = UIEdgeInsetsMake(0.0, 0.0, 0, -15);
重要提示:拆分两个邻居之间的插图;如果将整个插图应用到一个边缘,很明显按钮在空白空间中重叠 - 一个按钮获得所有间隙触摸。即使将这样的调整分开,两边都是-40,水龙头肯定会出现错误按钮。 -15或-20是使用此技术时最常考虑的。
IMPORTANT: Split the inset between the two neighbors; if apply the entire inset to one edge, it will become obvious that the buttons are overlapping in the "blank" space - one button gets all of the "gap" touches. Even when "split" the adjustment like this, at -40 on both edges, the tap will definitely go to wrong button sometimes. -15 or -20 is the most to consider using with this technique.
通过应用此方法,按钮甚至可以在四个方向上移动。
By applying this method, the button could even be moved around in four directions.
这篇关于如何调整rightBarButtonItems中两个UIBarButtonItem之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!