Можно ли поставить центральную кнопку на UIToolBar на iPhone? - PullRequest
1 голос
/ 22 ноября 2010

Я хочу поместить кнопку на UIToolBar в центральное положение. Какие изменения я должен сделать в следующем коде?

CGRect toolbarFrame = CGRectMake(0, 0, self.view.frame.size.width, 44);
UIToolbar *mytoolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
mytoolbar.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
mytoolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"button 1"
                           style:UIBarButtonItemStylePlain target:self action:nil];
NSMutableArray *tools = [[NSMutableArray alloc] initWithObjects:button,nil];
[mytoolbar setItems:tools];
[self.view addSubview:mytoolbar];

Ответы [ 2 ]

5 голосов
/ 22 ноября 2010

Я думаю, что если вы создадите два UIBarButtonItem, используя initWithBarButtonSystemItem: UIBarButtonSystemItemF FlexibleSpace, и поместите один перед кнопкой, а другой после - вы получите желаемое центрирование ...

1 голос
/ 22 февраля 2012

Создать одну кнопку панели типа UIBarButtonSystemItemFlexibleSpace.

UIBarButtonItem *spaceButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                             target:nil action:nil];
UIBarButtonItem *button =
    [[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain
                             target:self action:nil];
NSMutableArray *tools =
    [[NSMutableArray alloc] initWithObjects:spaceButton, button, spaceButton,nil];
[mytoolbar setItems:tools];
...