Create blank space between navigation bar items?

I spent several hours trying to figure it out to the end. My final option is to simply create empty shortcuts to create space, but I feel like their cleaner way.

Basically, I have three buttons, and we are trying to create a fixed space between them for accuracy. Each button is programmatically added.

I found this code:

UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20.0f; // or whatever you want

( Source )

But how do you assign this piece of code to a specific button?

+4
source share
3 answers

, . . UIBarButtonSystemItemFixedSpace. , , , . UIBarButtonItem ( , ), . ( leftBarButtonItems):

// Create "Normal" buttons items:
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"1" style:UIBarButtonItemStylePlain target:Nil action:nil];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"2" style:UIBarButtonItemStylePlain target:Nil action:nil];    
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithTitle:@"3" style:UIBarButtonItemStylePlain target:Nil action:nil];

// Create "Spacer" bar button items 
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20.0f; // or whatever you want
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

self.navigationItem.leftBarButtonItems = @[button1, fixedItem, button2, flexibleItem, button3];

, , items:

self.toolbar.items = @[button1, fixedItem, button2, flexibleItem, button3];
+8

Interface Builder. Flexible Fixed Space Bar, . : UIView . UIView , .

enter image description here

+4

You would add this button, potentially several times, to an array with other buttons. You will then set this array as a navigation item rightBarButtonItems(or leftBarButtonItems) or your control manager.

0
source

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


All Articles