3

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

我了解并使用灵活间距,当我需要平衡 UIToolbar 上的按钮时,它非常有用,但只有两个按钮,我似乎无法完美地居中中间按钮。我什至已经初始化了 view.toolbarItems 数组

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

并设置 fixed.width = right_button.width ...但是,center_button 永远不会完全居中。

4

3 回答 3

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];

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

于 2011-06-07T04:16:46.607 回答
1

问题是,UIBarButtonItem 的宽度属性似乎总是​​为零。(我认为这是因为系统将零用作标志以使其成为“适当的”宽度。弹性空间也是如此。)您可能需要做的是在您的右键中使用图像(即方式,您知道它的宽度是多少),并用“左按钮”替换您的固定空间,该按钮使用与右按钮大小相同的透明图像。

于 2010-09-27T20:07:27.103 回答
0

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

于 2010-05-19T05:59:20.257 回答