UIToolbar UIBarButtonItem对齐问题

Lut*_*ker 3 iphone cocoa-touch uitoolbar uikit uibarbuttonitem

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

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

NSArray *items = [[NSArray alloc] initWithObjects:fixed, flex, center_button, flex, right_button, nil];
Run Code Online (Sandbox Code Playgroud)

并设置fixed.width = right_button.width ...但仍然,center_button永远不会完美居中.

oct*_*cty 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];
Run Code Online (Sandbox Code Playgroud)

迟到的答案......我希望这仍然有用.