如何在 iOS 通用应用程序的 UIToolbar 中对齐 UIBarButtonItem?

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

How to align UIBarButtonItem in UIToolbar in iOS universal app?

iosxcodeswiftautolayoutuibarbuttonitem

提问by Blaszard

I want to put two UIBarButtonIteminstances to UIToolbarin my iOS universal app, on which the first item is on the left-edge of the toolbar and the second is on the center. However, it looks like this cannot be done in Interface Builder, because the auto layout doesn't support forcing constraints on UIBarButtonItemclass due to the fact that it is not a subclass from UIView.

我想在我的 iOS 通用应用程序中放置两个UIBarButtonItem实例UIToolbar,其中第一个项目位于工具栏的左边缘,第二个位于中心。但是,这似乎无法在 Interface Builder 中完成,因为自动布局不支持对UIBarButtonItem类施加强制约束,因为它不是来自UIView.

Notice that I don't want to put the first on the left-edge and the second is on the right-edge - I have to put the second button at the center. Also, I don't have a plan to use the third button in order to align them as left, center, and right at equal interval.

请注意,我不想将第一个放在左边缘而第二个放在右边缘 - 我必须将第二个按钮放在中心。另外,我没有计划使用第三个按钮来将它们以相等的间隔对齐为左、中和右。

Is it still possible to add such constraint in this situation in storyboard? I use Xcode 6 beta 5.

在这种情况下,是否仍然可以在情节提要中添加此类约束?我使用 Xcode 6 beta 5。

回答by Imanou Petit

You can do it all in your Storyboard.

您可以在故事板中完成所有操作。

Select a Bar Button Itemin the Object library and drag it into your Toolbar. Add a Flexible Space Bar Button Itemat its right. Add your second Bar Button Itemat its right. Then, add a second Flexible Space Bar Button Itemat its right.

Bar Button Item在对象库中选择一个并将其拖到您的Toolbar. Flexible Space Bar Button Item在其右侧添加一个。Bar Button Item在其右侧添加您的第二个。然后,Flexible Space Bar Button Item在其右侧添加第二个。

The result will look like this in Interface Builder:

结果在 Interface Builder 中将如下所示:

enter image description here

在此处输入图片说明

If necessary, you can add an extra Fixed Space Bar Button Itemat the right edge of your Toolbarto be sure that your second Bar Button Itemis really centered.

如有必要,您可以Fixed Space Bar Button Item在右边缘添加额外的内容,Toolbar以确保您的第二个Bar Button Item确实居中。

User lxt gives another example of this in answer for a similar question here.

用户LXT给出了答案类似的问题的另一个例子在这里

Edit:Be sure that your UIToolbar has good constraints before proceeding!

编辑:在继续之前,请确保您的 UIToolbar 具有良好的约束!

回答by Mohamad Chami

you can do this programmatically

你可以以编程方式做到这一点

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3" style:UIBarButtonItemStylePlain target:self action:nil];

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil];

check this link if you want more details

如果您想了解更多详情,请查看此链接

How to place UIBarButtonItem on the right side of a UIToolbar?

如何将 UIBarButtonItem 放在 UIToolbar 的右侧?

回答by Arvind

I think you should try Flexible bar button space item and for more understating i am sharing a link, so have look of it might this help you out

我认为你应该尝试灵活的栏按钮空间项目,为了更加低调,我正在分享一个链接,所以看看它可能会帮助你

UIToolbar and Flexable/Fixed bar button items

UIToolbar 和 Flexable/Fixed bar 按钮项

回答by Chetan

Code to add toolbar :

添加工具栏的代码:

Here fixed width can be variable. Accordingly you can keep it as far as you want so as to keep the second button in centre.

这里固定宽度可以是可变的。因此,您可以尽可能地保持它,以便将第二个按钮保持在中心。

UIBarButtonItem *fixedItemSpaceWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    fixedItemSpaceWidth.width = 200.0f; // or whatever you want

    UIBarButtonItem *doneBarButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonAction:)];

            UIBarButtonItem *cancelBarButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonAction:)];

            // Initialise toolabr
            UIToolbar *toolbar          = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-44, 320, 44)];
            //toolbar.barStyle = UIBarStyleBlack;
            toolbar.items               = [NSArray arrayWithObjects:cancelBarButton,fixedItemSpaceWidth,doneBarButton, nil];
        [self.view addSubview:toolbar];