xcode 在工具栏中放置条形按钮项

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

Position a bar button item in a toolbar

iphoneobjective-ciosxcodeuitoolbar

提问by CrystalBlue

I have a tool bar at the top and bottom of my application and I need to create buttons to put into the toolbars. The ones designing this application would like space placed between the buttons on the toolbar. Aside from manually coding in a position change for the buttons, is there a better way to accomplish this through Interface Builder?

我在应用程序的顶部和底部有一个工具栏,我需要创建按钮以放入工具栏中。设计此应用程序的人希望在工具栏上的按钮之间放置空间。除了手动编码按钮的位置更改之外,是否有更好的方法通过 Interface Builder 完成此操作?

回答by EmptyStack

You can add a bar button of type UIBarButtonSystemItemFlexibaleSpacein the place where you want the space.

您可以在需要空间的位置添加UIBarButtonSystemItemFlexibaleSpace类型的条形按钮。

UIBarButtonItem *barButton1 = ...
UIBarButtonItem *barButton2 = ...

UIBarButtonItem *flexibleSpaceBarButton = [[UIBarButtonItem alloc] 
                           initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                target:nil
                                                action:nil];

toolbar.items = [NSArray arrayWithObjects:barButton1, 
                                          flexibleSpaceBarButton, 
                                          barButton2, nil];

回答by Ern? Simonyi

As EmptyStack wrote, you can use the UIBarButtonItemFlexibleSpace to create spaces between buttons. You can either use the code written above or do it in the Interface Builder. In IB you will have to place the toolbar and then you can find toolbar items if I remember correcly around the end of the items list. So you can add a flexible space item to the toolbar, play around with its width and then add a 'real' bar button to the toolbar. You can declare outlets and assign actions to the barbuttons just like for normal UIButtons.

正如 EmptyStack 所写,您可以使用 UIBarButtonItemFlexibleSpace 在按钮之间创建空间。您可以使用上面编写的代码,也可以在 Interface Builder 中进行。在IB中,您必须放置工具栏,然后如果我记得在项目列表的末尾正确地找到工具栏项目。因此,您可以向工具栏添加一个灵活的空间项目,调整其宽度,然后向工具栏添加一个“真实”栏按钮。您可以像普通 UIButton 一样声明插座并为条形按钮分配操作。