xcode 向 UIBarButtonItem 添加约束

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

Adding constraints to a UIBarButtonItem

xcodeswiftautolayoutuinavigationbaruibarbuttonitem

提问by Lukesivi

Xcode is not allowing me to put constraints on my BarButtonItemon a ViewController in the MainStoryBoard. It's appearing too far to the left where it's unreadable as shown in the image.

Xcode 不允许我对BarButtonItemMainStoryBoard 中的 ViewController施加约束。它出现在左边太远的地方,如图所示。

The image below shows where the BarButtonItemis. It's supposed to say "Item". I also looked on the preview split screen on Xcode and it looked fine for the iPhone6 but not for the iPhone 4 (it's half cut on the iPhone 4 but here its 3/4s cut). Here I'm running for the iPhone 6 and i get that.

下图显示了它的BarButtonItem位置。它应该说“项目”。我还查看了 Xcode 上的预览分屏,它对于 iPhone6 来说看起来不错,但对于 iPhone 4 来说却不是(它在 iPhone 4 上被剪了一半,但在这里是 3/4 秒)。在这里,我正在运行 iPhone 6,我明白了。

How do I add constraints in Xcode or how do I add the constraints programmatically to this BarButtonItem?

如何在 Xcode 中添加约束或如何以编程方式将约束添加到此 BarButtonItem?

enter image description here

在此处输入图片说明

Any help is much appreciated.

任何帮助深表感谢。

采纳答案by luckyShubhra

Drag and drop navigation bar in your xib / story board

在您的 xib / 故事板中拖放导航栏

enter image description here

在此处输入图片说明

U can add constraints to this navigation bar as per your requirement.

您可以根据您的要求向此导航栏添加约束。

Then add Bar Button Item to your navigation bar

然后将 Bar Button Item 添加到导航栏

enter image description here

在此处输入图片说明

Bar Button Item takes it default position u cannot incorporate autolayout there.

条形按钮项采用默认位置,您不能在那里合并自动布局。

Although if u want positions of Bar Button Item Fixed or Flexible u can make use of Fixed Space Bar Button Item or Flexible Space Bar Button Item.

虽然如果你想要固定或灵活的条形按钮项目的位置,你可以使用固定空格键按钮项或灵活空格键按钮项。

enter image description here

在此处输入图片说明

If positions are fixed drag Fixed Space Bar Button between your two Bar Button Item's. And if positions is not fixed its flexible drag Flexible Space Bar Button Item between your two Bar Button Item's.

如果位置是固定的,请在您的两个条形按钮项之间拖动固定空间条形按钮。如果位置不固定,则在您的两个条形按钮项之间灵活拖动灵活的空格条按钮项。

Happy Coding.. :)

快乐编码.. :)

回答by Orkhan Alizade

Try this:

尝试这个:

let rightConstraint = NSLayoutConstraint(item: your_item_here, attribute: .Right, relatedBy: .Equal, toItem: your_item_here, attribute: .Left, multiplier: 0, constant: 1)
your_item_here.addConstraint(rightConstraint)

P.Sif you want to add constraint to your UIBarButtonItem, it will rise an error

PS如果你想给你的 UIBarButtonItem 添加约束,它会引发一个错误

Value of type 'UIBarButtonItem' has no member 'addConstraint'

P.S.S

PSS

Because of it is not a view class, you can't apply constraints to it.

因为它不是视图类,所以不能对其应用约束。