Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 452
Carter Pape Avatar asked May 05 '12 14:05

Carter Pape


People also ask

What does constrain to margins mean?

The “Constrain to margins” checkbox determines whether constraints to the superview use the superview's margins or its edges. The lower portion of the popover lets you set the item's width or height. The Width and Height constraints default to the current canvas size, though you can type in different values.

How do you add constraints in storyboard IOS?

Open the Align menu with the yellow button selected and check Horizontally in Container, then click Add 1 Constraint. Now, select both buttons at the same time using the Shift key and, in the Align menu, check Leading Edges. Again, actually install the constraint by clicking Add 1 Constraint.


1 Answers

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:

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:

window 2

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

window 3

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

window 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 (e.g. German names tend to get pretty long).

like image 125
Caleb Avatar answered Nov 09 '22 05:11

Caleb