ios 对齐 UIToolBar 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/602717/
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-08-30 15:42:44  来源:igfitidea点击:

Aligning UIToolBar items

ioscocoa-touchuitoolbar

提问by 4thSpace

I have three UIBarButtonItemcreated as below. They align left and I'd like to align center so there isn't a gap on the right side. I don't see an align property on UIToolBar. Is there another way to accomplish this?

UIBarButtonItem创建了三个,如下所示。它们向左对齐,我想居中对齐,因此右侧没有间隙。我没有看到 align 属性UIToolBar。有没有另一种方法来实现这一点?

//create some buttons
UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAbout:)];
[toolbar setItems:[NSArray arrayWithObjects:settingsButton,deleteButton,aboutButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];

回答by nduplessis

Add two UIBarButtonSystemItemFlexibleSpace items to your toolbar, to the left and right of your items

将两个 UIBarButtonSystemItemFlexibleSpace 项目添加到您的工具栏,在您的项目的左侧和右侧

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]];

Adding these as you would any other toolbar items will distribute space evenly between the two of them.

像添加任何其他工具栏项目一样添加这些将在它们两个之间均匀分布空间。

回答by Nycen

This can also be done right from a storyboard.

这也可以直接从故事板完成。

Just drag and drop items in the toolbar, and turn some of them into flexible or fixed space to get the desired effect. See the two examples below.

只需拖放工具栏中的项目,将其中的一些变成灵活或固定的空间即可获得所需的效果。请参阅下面的两个示例。

Evenly spaced

均匀分布的

Centered

居中

回答by Herman Schoenfeld

In Xamarin iOS

在 Xamarin iOS 中

Right aligned:

右对齐:

yourBar.SetItems(new [] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), yourButton }, false);

Center Aligned:

居中对齐:

var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
yourBar.SetItems(new [] { flexibleSpace, yourButton, flexibleSpace}, false);

回答by rgamber

Swift version:

迅捷版:

    let toolbar = UIToolbar(frame: CGRectMake(0, 0, viewController.view.frame.size.width, 35.0))
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: viewController, action: nil)
    let button1 = UIBarButtonItem(title: "A", style: UIBarButtonItemStyle.Plain, target: viewController, action: foo)
    let button2 = UIBarButtonItem(title: "B", style: UIBarButtonItemStyle.Plain, target: viewController, action: bar)
    let button3 = UIBarButtonItem(title: "C", style: UIBarButtonItemStyle.Plain, target: viewController, action: blah)
    toolbar.items = [button1, flexibleSpace, button2, flexibleSpace, button3]