UIToolbar UIBarButtonItem Alignment question
我需要创建一个具有两个UIBarButtonItems的UIToolbar。第一个按钮必须居中,第二个按钮必须右对齐。
我理解并使用灵活间距,当我需要在UIToolbar上平衡按钮时,它非常有用,但是只有两个按钮,我似乎无法完美地使中间按钮居中。我什至用
初始化了view.toolbarItems数组。
1 | NSArray *items = [[NSArray alloc] initWithObjects:fixed, flex, center_button, flex, right_button, nil]; |
并设置fixed.width = right_button.width ...但仍然,center_button永远不会完美居中。
我最近遇到了同样的问题,并通过创建一种伪造的UIBarButtonItem解决了该问题。正确对齐的关键是将左右按钮的
1 2 3 4 5 6 7 8 9 | [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]; |
最新答案...希望对您有所帮助。
问题是,UIBarButtonItem的width属性似乎始终为零。 (我认为这是因为系统将零用作标志来使其成为"适当的"宽度。伸缩空间是相同的方式。)您可能需要做的是在右按钮中使用图像(即这样,您知道其宽度将是多少),并用"左按钮"替换固定空间,该"左按钮"使用与右按钮大小相同的透明图像。
听起来您的工具栏不是屏幕的整个宽度,或者您的center_button不在其框架的中心。您是否尝试设置