2010-05-02 71 views
3

我需要创建一个具有两个UIBarButtonItems的UIToolbar。第一个按钮必须居中,第二个项目必须右对齐。UIToolbar UIBarButtonItem对齐问题

我理解并使用灵活的间距,当我需要平衡整个UIToolbar的按钮时,它的工作效果很好,但只有两个按钮,我似乎无法完美地居中中间按钮。我甚至有

NSArray *items = [[NSArray alloc] initWithObjects:fixed, flex, center_button, flex, right_button, nil]; 

初始化view.toolbarItems阵列,并设置fixed.width = right_button.width ......但尽管如此,center_button永远不会完全集中。

+0

请务必使用工具包进行标记,以便人们可以通过标签隐藏与其无关的问题。 – doublep 2010-05-02 18:41:34

回答

9

我最近有同样的问题,并通过创建一种假的UIBarButtonItem来解决它。在得到右对齐的关键是左右两个栏按钮的possibleTitles属性设置为相同的值,例如:

[right_button setPossibleTitles:[NSSet setWithObject:@"Abc"]]; 

// Create this fake (plain) button item on the left, to correct the alignment. 
UIBarButtonItem *fake = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease]; 
[fake setEnabled:NO]; 
[fake setPossibleTitles:right_button.possibleTitles]; 

// The array for your toolbar items 
NSArray *items = [[NSArray alloc] initWithObjects:fake, flex, center_button, flex, right_button, nil]; 

迟到的回答...我希望这仍然可以提供帮助。

+0

我现在无法测试这个 - 但很棒!我会尽快尝试一次,我有机会。 – 2011-06-14 21:44:05

+0

谢谢!使用possibleTitles允许我将我的中间播放按钮置于一组三个按钮中。我只是将外部按钮设置为相同的可能标题。 – 2011-08-18 17:08:37

0

听起来好像你的工具栏不是整个屏幕的宽度,或者你的center_button没有在它的框架中居中。你有没有试过设置center_button.imageInsets

+0

事实上,UIToolbar是UINavigationController的一部分,并在整个屏幕上延伸。中心按钮可以是任何UIBarButtonSystem项目,如果我有item,flex,item,flex,item,那么这些项目就可以工作得很好。只有当我想在最左角找到一个空白点时,问题才会体现出来。 – 2010-06-19 18:39:09

1

问题是,UIBarButtonItem的宽度属性似乎总是​​为零。 (我认为这是因为零被系统用作一个标志,使它成为“合适的”宽度,而flex空间也是一样)。你可能要做的是在你的右键中使用一个图像方式,你知道它的宽度将是什么),并用一个“左键”替换你的固定空间,使用与右键相同大小的透明图像。