简体   繁体   English

当我更改自动布局约束时,为什么我的 NSWindow 不调整大小?

[英]Why doesn't my NSWindow resize when I change auto layout constraints?

I think I have arrived at a decent understanding of how auto layout works in Interface Builder (ha).我想我已经对 Interface Builder (ha) 中的自动布局如何工作有了很好的理解。 I've got a .xib that says "No auto layout issues".我有一个 .xib,上面写着“没有自动布局问题”。 Here's two screenshots of it, with nothing selected and with things selected so you can get a sense of the constraints at work:这是它的两个屏幕截图,未选择任何内容和已选择的内容,以便您了解工作中的限制:

在此处输入图片说明 在此处输入图片说明

Now I would like to, eg, decrease the vertical spacing between the "Total thumbnail size:" label and the "Use TIFF" radio button matrix.现在我想,例如,减少“总缩略图大小:”标签和“使用 TIFF”单选按钮矩阵之间的垂直间距。 So I click on things to get that constraint selected, and I edit the "Constant" value of it to change it from 47 to, say, 30. When I do so, I expect that the window will shrink vertically by 17, so that all constraints continue to be obeyed.所以我点击一些东西来选择那个约束,然后我编辑它的“常量”值,将它从 47 更改为 30。当我这样做时,我希望窗口将垂直缩小 17,这样继续遵守所有限制条件。 Instead, it shifts the vertical position of the "Size & format" label, breaking its vertical constraints in so doing, and then complains about "Content priority ambiguities" involving a bunch of items;相反,它改变了“大小和格式”标签的垂直位置,这样做打破了它的垂直限制,然后抱怨涉及一堆项目的“内容优先级歧义”; here are screenshots of that state, showing the priority ambiguities:这是该状态的屏幕截图,显示了优先级的模糊性:

在此处输入图片说明 在此处输入图片说明

It wants me to change vertical compression resistance priorities and/or vertical hugging priorities to allow it to compress/expand something away from its intrinsic size;它希望我改变垂直压缩阻力优先级和/或垂直拥抱优先级,以允许它压缩/扩展某些东西而不是它的固有尺寸; but I don't want to do that.但我不想那样做。 I want all of those items to retain their intrinsic size, and I want the window to resize.我希望所有这些项目都保留其固有大小,并且我希望窗口调整大小。 I can resize the window manually, by -17, and move the various items into their proper positions, and then it says "No auto layout issues" again;我可以手动调整窗口大小 -17,并将各种项目移动到适当的位置,然后再次显示“没有自动布局问题”; so there is really nothing difficult or ambiguous about this situation except that for some reason it refuses to resize the window as it ought to.所以这种情况真的没有什么困难或模棱两可的,除了出于某种原因它拒绝按应有的方式调整窗口大小。 So, my question is: how do I avoid it doing this, and have it just resize the window and move things properly on its own?所以,我的问题是:我如何避免它这样做,并让它自己调整窗口大小并正确移动东西? I don't understand why it's so confused;我不明白为什么这么困惑; it seems to me that there is a clear, unambiguous chain of vertical constraints from the top to the bottom of the window, and changing one of those constraints ought to simply force the window to resize.在我看来,从窗口的顶部到底部有一条清晰、明确的垂直约束链,更改这些约束之一应该只是强制窗口调整大小。 I've tried using "Update Frames" but it doesn't seem to help.我试过使用“更新框架”,但它似乎没有帮助。

I think I have uploaded the .xib file, prior to the constraint change, to my Dropbox here: https://www.dropbox.com/s/vp90s1fh692i8pi/CopyThumbs.xib .我想我已经在约束更改之前将 .xib 文件上传到我的 Dropbox: https : //www.dropbox.com/s/vp90s1fh692i8pi/CopyThumbs.xib

I grabbed your xib and played around with a few things... I can only find two items that would be causing problems.我抓住了你的 xib 并尝试了一些东西......我只能找到两个会导致问题的项目。

But first, a couple tips (based on my experiences):但首先,有一些提示(根据我的经验):

  • Start by making your window larger than you'll need首先让你的窗口比你需要的大
    • with your layout, make it maybe 600 x 600使用您的布局,使其可能为600 x 600
  • As you add UI elements add only Vertical and Leading constraints (we can center the Cancel/Copy buttons later添加 UI 元素时仅添加垂直和前导约束(我们可以稍后将取消/复制按钮居中
    • of course, I also mean Horizontal inter-element constraints当然,我也指的是横向元素间约束
  • For wrapping multi-line labels (ie your "Choose how you want..." label), give it a specific Width constraint对于包装多行标签(即“选择你想要的方式...”标签),给它一个特定的宽度约束
    • 285 is a good value for your layout... you can adjust this later as desired 285对您的布局来说是一个很好的价值……您可以稍后根据需要进行调整
  • Don't give a Bottom constraint yet不要给出底部约束

Your xib now looks like this (I left your Cancel button Leading as you had it >= 50 ):你的 xib 现在看起来像这样(我把你的取消按钮保持在你拥有的>= 50 ):

在此处输入图片说明

Now you can tweak your vertical spacing constraints, without worrying about the window frame sizing.现在,您可以调整垂直间距约束,而无需担心窗口框架的大小。

Once you're happy with the layout, you only need one Trailing constraint (on your multi-line label) and one Bottom constraint (on the Cancel button).一旦您对布局感到满意,您只需要一个 Trailing 约束(在您的多行标签上)和一个底部约束(在 Cancel 按钮上)。

Add those constraints, and your window frame should resize itself.添加这些约束,您的窗口框架自行调整大小。 If it doesn't, selecting "Update Frames" should fix it.如果没有,选择“更新框架”应该可以修复它。

The two problem items that I mentioned... you have Content Hugging Priority on both "Matte thumbnails..." and "Frame thumbs..." set to 250 .我提到的两个问题项目...您将“Matte thumbs...”和“Frame thumbs...”的 Content Hugging Priority 设置为250 Those should both be at their default of 750 .这些都应该是默认值750

Your Cancel/Copy buttons should horizontally center themselves, with your current constraints.您的“取消/复制”按钮根据您当前的约束水平居中。 I'd suggest, though, putting them in a StackView, and then constraining that StackView Top to the element above it, and Bottom to the Superview... and thenHorizontally Centered.不过,我建议将它们放在 StackView 中,然后将该 StackView Top 约束到它上面的元素,将 Bottom 约束到 Superview ......然后水平居中。


Edit编辑

Note: I don't work for Apple... these comments are simply my anecdotal observations.注意:我不为 Apple 工作……这些评论只是我的轶事观察。

First, Interface Builder is (as we all know) not the same as run-time output.首先,Interface Builder的是(大家都知道)一样的运行时输出。 It's close, but it's certainly not identical.它很接近,但肯定不完全相同。

Second, IB makes a lot of assumptions -- while at the same time, it has no idea what we're going to do next.其次,IB 做了很多假设——同时,它不知道我们接下来要做什么。

A perfect example from iOS development: iOS开发的一个完美例子:

If, in IB, I add a UIScrollView to a view without adding any content to it, IB will endlessly complain about Content Size Ambiguity ... and won't even position the view to reflect the constraints.如果在 IB 中,我向视图添加UIScrollView而不向其添加任何内容,IB 将无休止地抱怨Content Size Ambiguity ......甚至不会定位视图以反映约束。

That's because IB doesn't know that I'm going to add subviews (with proper constraints) at run-time.那是因为 IB 不知道我将在运行时添加子视图(具有适当的约束)。 And the only way to get rid of the Red "Error" messages is to add a subview in IB which I would then have to immediately remove at run-time (ugh).摆脱红色“错误”消息的唯一方法是在 IB 中添加一个子视图,然后我必须在运行时立即删除它(呃)。

IB also tends to complain / warn about "Fixed width constraints may cause clipping" and, as you're experiencing, "Localization Issue: Trailing constraint is missing." IB 还倾向于抱怨/警告“固定宽度约束可能会导致裁剪”,并且正如您所遇到的,“本地化问题:缺少尾随约束”。

It doesn't matter if my label is static (text will never change) or if I have the constraints set exactly how I want them.我的标签是静态的(文本永远不会改变)或者我是否按照我想要的方式设置了约束都没有关系。 IB is going to try its darnedest to convince me that I need to change things. IB 将尽最大努力让我相信我需要改变。

This is one of the reasons many people move away from using Storyboards / XIBs.这是许多人不再使用 Storyboards / XIBs 的原因之一。 For my job, we don't use any XIBs, and the only Storyboard we use is for the LaunchScreen (iOS).对于我的工作,我们不使用任何 XIB,我们使用的唯一 Storyboard 用于 LaunchScreen (iOS)。

I might lay things out in IB to start, and to get a good idea of how I want my layout to look, but then I write it all in code for the end result.我可能会在 IB 中布置一些东西来开始,并很好地了解我希望我的布局看起来如何,但随后我将所有这些都写在代码中以获得最终结果。

Not that I'm saying this approach is better... plenty of times when I think to myself "Hey, I can drag/drop UI elements, add constraints and IBOutlet / IBAction connections, and I'd be done already!"并不是说我说这种方法更好……很多时候当我对自己说“嘿,我可以拖放 UI 元素,添加约束和IBOutlet / IBAction连接,我已经完成了!”

Again, though, just my personal thoughts on the topic.不过,再次,只是我对这个话题的个人想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当通过自动布局调整大小时,如何为 NSWindow 的大小更改设置动画? - How do I animate a NSWindow's size change when it gets resized by Auto Layout? 调整窗口大小时,为什么我的NSWindow中的NSViews不调整大小? - Why aren't my NSViews in my NSWindow resizing when the window is resized? 如何强制我的应用对我的自动版式约束使用反文本方向? - How can I force my app to use the inverse text direction for my Auto Layout constraints? 主要的NSWindow阴影不像儿童窗户那样变化 - Main NSWindow shadow doesn't change like child windows SetOpaque:NO时10.9中的自定义NSWindow不显示阴影 - Custom NSWindow in 10.9 Doesn't show shadow when SetOpaque:NO NSTextView 在放置在 NSWindow 顶部附近时不起作用 - NSTextView doesn’t work when placed near the top of an NSWindow 我可以使用“自动布局”来根据标签大小调整窗口大小吗? - Can I use Auto Layout to resize my window according to a label size? 自动布局NSWindow在IB仿真器中遵守最小尺寸,但在实际运行时不遵循 - Auto layout NSWindow respects minimum size in IB simulator, but not when running for real 如何使窗口缩放尊重自动布局约束? - How can I make window zooming respect auto layout constraints? NSWindow调整大小时调整表列大小 - Resize Table Column when NSWindow resizes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM