如何使用 Xcode 的自动布局调整视图大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12109365/
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
How to have a view resize using Xcode's auto layout
提问by codingFriend1
I am using Xcode's auto layout feature for the first time in a project where I have several NSPopUpButtons. Now what I want to achieve is to have two popUpButtons in a row together with their labels and when the window is resized I want both popUpButtons to adjust their width while keeping the horizontal spacing between each other.
我在一个有多个 NSPopUpButton 的项目中第一次使用 Xcode 的自动布局功能。现在我想要实现的是将两个 popUpButtons 与它们的标签一起排成一行,当窗口调整大小时,我希望两个 popUpButtons 调整它们的宽度,同时保持彼此之间的水平间距。
However no matter how I apply the constraints I just don't get the popUpButtons to change their size with the window. They will always break their horizontal spacing constraints and just increase/decrease the spacing to the labels. I hope it gets a bit clearer what I have done from this screenshot:
然而,无论我如何应用约束,我都不会让 popUpButtons 随窗口改变它们的大小。他们总是会打破他们的水平间距限制,只是增加/减少标签的间距。我希望从这个屏幕截图中可以更清楚地了解我所做的事情:
I have set the spacings between the labels and the popUpButtons to fixed values with 1000 priority and have set the width constraints fo the popUpButtons to be greater or equal to the initial size.
我已将标签和弹出按钮之间的间距设置为优先级为 1000 的固定值,并将弹出按钮的宽度约束设置为大于或等于初始大小。
How must I set my constraints to have the popUpButtons resize?
我必须如何设置约束才能调整 popUpButtons 的大小?
回答by codingFriend1
While writing this question I realized what the trick is:
在写这个问题时,我意识到了诀窍是什么:
In the size inspector of the NSPopUpButton I had to reduce the Content Hugging Priority
.
在 NSPopUpButton 的大小检查器中,我不得不减少Content Hugging Priority
.
Obviously this controls how closely the view wants to 'hug' its content. So when the hugging priority is higher than the resize priority the view will not want to increase its size because that would mean to have more empty space between its bounds and its content.
显然,这控制了视图想要“拥抱”其内容的紧密程度。因此,当拥抱优先级高于调整大小优先级时,视图将不希望增加其大小,因为这意味着在其边界和其内容之间有更多的空白空间。
Then in my special case, I could also pin both NSPopUpButtons to have the same width and voilà: the popUpButtons will perfectly resize while keeping the spacing constant.
然后在我的特殊情况下,我还可以将两个 NSPopUpButtons 固定为具有相同的宽度和瞧:popUpButtons 将完美地调整大小,同时保持间距不变。