代码之家  ›  专栏  ›  技术社区  ›  Viral Narshana

是否可以在iPhone的U工具栏上居中放置按钮?

  •  1
  • Viral Narshana  · 技术社区  · 14 年前

    我想把一个按钮放在中间位置的U工具栏上。我必须在下面的代码中做什么更改?

    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 回复  |  直到 13 年前
        1
  •  5
  •   Andy Milburn    14 年前

    我认为,如果你创建两个uibarbuttonitem,使用initwithbarbuttonsystememittem:uibarbuttonsystemflexibblespace,将一个放在你的按钮之前,另一个放在你想要的中心之后……

        2
  •  1
  •   Gilles 'SO- stop being evil'    13 年前

    创建一个条形图按钮 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];