UIToolbar UIBarButtonItem Alignment Question

I need to create a UIToolbar with two UIBarButtonItems. The first button should be centered, and the second element should be aligned to the right.

I understand and use flexible spacing, and it works great when I need to balance the buttons on the UIToolbar, but with only two buttons, I cannot distinguish the center middle button. I even initialized the view.toolbarItems array using

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

and set fixed.width = right_button.width ... but still the center_button center will never be perfectly centered.

+3
source share
3 answers

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

... , .

+9

, UIBarButtonItem . ( , , , "" . .) , ( , , ), " ", , .

+1

Source: https://habr.com/ru/post/1743730/


All Articles