UIToolBar上面添加间隔一样的UIBarButtonItem

In Xcode, If you want to add items to the UIToolbar, you need to create an NSArray which stores all the items and put them to the UIToolbar using the setItems method.

If you want to align one items on the left and the another one on the right. you need to create a space item in between the two button in the array. That’s just like adding a Flexible Space Bar Button Item in Interface Builder.

01

// Initialize the bar items

02

leftButton = [[UIBarButtonItem alloc]

03

            initWithTitle:@"Left"

04

            style:UIBarButtonItemStyleBordered

05

            target:self

06

            action:@selector(leftButtonClicked:)];

07

rightButton = [[UIBarButtonItem alloc]

08

            initWithTitle:@"Right"

09

            style:UIBarButtonItemStyleBordered

10

            target:self

11

            action:@selector(rightButtonClicked:)];

12

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]

13

            initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

14

            target:nil

15

            action:nil];

16

                 

17

// Add the items to the toolbar

18

[toolbar setItems:[NSArray arrayWithObjects:leftButton, flexibleSpace, rightButton, nil]];

19

 

20

// Add the toolbar view to the controller view

21

[self.view addSubview:toolbar];

你可能感兴趣的:(UIToolBar上面添加间隔一样的UIBarButtonItem)