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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:48:54  来源:igfitidea点击:

How to add a button on the right side of the toolbar?

iphoneobjective-ciosxcodeuibutton

提问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 ?

但是,此按钮仅出现在工具栏的左侧。是否可以将其放在工具栏的右侧?

enter image description here

在此处输入图片说明

回答by IronManGill

Here is the method to add the UIBarButtonItemon 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”的项目。

enter image description here

在此处输入图片说明

回答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")