xcode 如何在工具栏右侧添加按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12870780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a button on the right side of the toolbar?
提问by SmartTree
I created a toolbar programmatically:
我以编程方式创建了一个工具栏:
UIToolbar *boolbar = [UIToolbar new];
boolbar.barStyle = UIBarStyleDefault;
boolbar.tintColor = [UIColor orangeColor];
[boolbar sizeToFit];
And then added a button to it:
然后给它添加了一个按钮:
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];
However, this button appears only at the left side of the toolbar. Is it possible to put it on the right side of the toolbar ?
但是,此按钮仅出现在工具栏的左侧。是否可以将其放在工具栏的右侧?
回答by IronManGill
Here is the method to add the UIBarButtonItem
on the right side of the toolbar.
这是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];
OR
或者
If you are attempting to do it from the XIB , then .
如果您尝试从 XIB 执行此操作,则 .
Insert an item which has identifier being "flexible space".
插入一个标识符为“flexible space”的项目。
回答by lifeisfoo
In Swift
在斯威夫特
let btn1 = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: "btn1Pressed"
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let btn2 = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: "btn2Pressed")