cod*_*007 75

从iOS 5开始,您现在可以使用setLeftBarButtonItems:animated:或执行此操作setRightBarButtonItems:animated:

  • 现在这比当前选择的答案更有意义. (6认同)

小智 48

您必须使用UIToolbar并使用按钮设置工具栏:

// create a toolbar where we can place some buttons
UIToolbar *toolbar = [[UIToolbar alloc]
                        initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemSave
    target:self
    action:@selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];

// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
    target:nil
    action:nil];
[buttons addObject:spacer];

// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
    target:self
    action:@selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];

// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];

// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                           initWithCustomView:toolbar];
Run Code Online (Sandbox Code Playgroud)

  • 显然是从http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/中逐字复制的 (7认同)
  • 好的工作是逐字复制的,因为该网站目前正在下降! (3认同)