Should I use view constraints or minimum window size?

I'm making an app for use on OS X and I'm noticing how useful the new constraints feature is in the Interface Builder (which is built into Xcode now, of course). It's so useful and dynamic in fact that I'm questioning whether or not I should set a minimum window size or just rely on the constraints of my windows to set the minimums and maximums themselves.

I have a feeling that OS X takes minimum and maximum window sizes into consideration with matters other than limiting window size in some way and that it may be useful to set the minimums and maximums for that sake, but I also feel like it might be good style to rely on the constraints to dynamically set minimum and maximum window heights because of their dynamic behavior and all. For example, if I decide to change the minimum width of a control with constraints, I don't have to worry about also going to change the minimum window's minimum width.

Another even more crucial example of the benefits of relying on constraints to set the minimum and maximum window sizes is that if the user changes something like text size, the affected controls in my application are able to change their size constraints dynamically, but a statically set minimum and maximum window size would ruin that dynamic behavior.

Once again, all of these benefits should also be considered with the fact in mind that OS X might take minimum and maximum window sizes into consideration in some way and that it may be useful to set the minimums and maximums for that reason; I'm just not sure if OS X takes them into consideration, and if they do, how it uses them.

I've looked through Apple's documentation and cannot find anything that provides a satisfying answer.


The best thing to do in a situation like this is to try it out yourself. It took no more than two minutes to create a new application with a single window and a few controls. You don't need to add any code at all if you just want to play with a resizable window:

窗口1

This window has no minimum size and no constraints, and the problem is immediately obvious. You can resize the window so that it looks like this:

窗口2

Adding some constraints between the buttons shows the promise that constraints provide. Now the window looks like this at its smallest size:

窗口3

A couple more constraints on the label finally gave the desired result:

窗口4

That's great, but it took a bit of work to get there. I didn't add a complete set of constraints -- a vertical constraint between the two right hand buttons would have been redundant since there's already one between the buttons on the left. For a window with many controls, setting up enough constraints to cover all the views could be: a) very useful and worthwhile, or b) a pain in the butt and of little extra value. It depends on your situation. A simpler scheme is to just add up the heights of the controls that might overlap (two buttons and the label) and the desired spaces between them, and then set that as the minimum height for the window.

I can see either strategy being useful, depending on the window content. In fact, I don't think they're really two separate strategies at all... setting the minimum window dimensions is really just another kind of constraint that you're adding. For example, there may be a size below which your window would just look silly or not be very useful, so you could set the minimum window size to those dimensions. At the same time, you might want to set constraints between buttons to prevent overlapping controls in localized versions (eg German names tend to get pretty long).

链接地址: http://www.djcxy.com/p/49960.html

上一篇: 正确调整视图上的行大小

下一篇: 我应该使用视图约束还是最小窗口大小?