iphone - 如何在工具栏右侧添加一个按钮?

标签 iphone objective-c ios xcode uibutton

我以编程方式创建了一个工具栏:

UIToolbar *boolbar = [UIToolbar new];
    boolbar.barStyle = UIBarStyleDefault;
    boolbar.tintColor = [UIColor orangeColor];
    [boolbar sizeToFit];

然后给它添加一个按钮:

UIBarButtonItem *cancelleftBarButton =[[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(tapBackGround:)];

cancelleftBarButton.tintColor = [UIColor orangeColor];

NSArray *array = [NSArray arrayWithObjects:cancelleftBarButton, nil];
[boolbar setItems:array animated:YES];

但是,这个按钮只出现在工具栏的左侧。是否可以将它放在工具栏的右侧?

enter image description here

最佳答案

下面是在工具栏右侧添加UIBarButtonItem的方法。

UIBarButtonItem *leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem1Pressed:)] autorelease];

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];

UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem2Pressed:)] autorelease];

如果您尝试从 XIB 执行此操作,那么。

插入一个标识符为“灵活空间”的项目。

enter image description here

关于iphone - 如何在工具栏右侧添加一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870780/

相关文章:

c++ - Apple 的 Using C++ with Objective-C 文章不见了...谁能给我一篇新文章,或者帮我找到旧文章?

ios - SKLabelNode 触摸更新

ios - 为什么我的 .plist 没有显示保存的数据?

iphone - UITableViewController单元格设计问题

iphone - 如何以编程方式添加 UISearchDisplay Controller ?

iphone - 向我自己的类(class)添加类别

iphone - iOS AirPrint - 打印多个 URL

iphone - 我的应用程序文本的配置文件

ios - 如何使用 Swift 更改 MKAnnotation 颜色?

ios - 使用[popviewcontroller]传回数据,并重新加载数据