Kalle
Kalle

Reputation: 13346

Resize specific view with superview

I have this: Typical form style

Users want to resize the window and I want the description field to resize with super. (Ideally I'd want keywords and description to resize equally but let's not get crazy.)

I managed to make this work in the past by selecting the top half and adding constraints to left-top-right, then selecting the bottom half and adding constraints to left-bottom-right, like this:

Autolayout top half

Autolayout bottom half

and then finally

Description fix

but this suddenly stopped working. The window suddenly only allows resizing horizontally, and not vertically.

Edit: output from constraintsAffectingLayoutForOrientation:NSLayoutConstraintOrientationVertical from the description scroll view:

(
    "<NSContentSizeLayoutConstraint:0x6100002b5600 V:[NSTextField:0x608000194ec0(17)] Hug:750 CompressionResistance:750>",
    "<NSLayoutConstraint:0x608000098ba0 V:[NSTextField:0x608000194ec0]-(9)-[NSTextField:0x608000195060]>",
    "<NSContentSizeLayoutConstraint:0x6100002b57e0 V:[NSTextField:0x608000195060(17)] Hug:750 CompressionResistance:750>",
    "<NSLayoutConstraint:0x6080000969e0 V:[NSTextField:0x608000195060]-(72)-[NSTextField:0x6080001949e0]>",
    "<NSContentSizeLayoutConstraint:0x6100002b54e0 V:[NSTextField:0x608000194d20(17)] Hug:750 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x6100002b53c0 V:[NSTextField:0x608000194b80(17)] Hug:750 CompressionResistance:750>",
    "<NSLayoutConstraint:0x60800009a0e0 V:[NSTextField:0x608000194910]-(30)-[NSTextField:0x608000194b80]>",
    "<NSContentSizeLayoutConstraint:0x6100002b50c0 V:[NSTextField:0x608000194910(17)] Hug:750 CompressionResistance:750>",
    "<NSLayoutConstraint:0x60800009a220 V:[NSTextField:0x608000194840]-(NSSpace(8))-[NSTextField:0x608000194910]>",
    "<NSContentSizeLayoutConstraint:0x6100002b4fa0 V:[NSTextField:0x608000194840(17)] Hug:750 CompressionResistance:750>",
    "<NSLayoutConstraint:0x60800009a3b0 V:|-(22)-[NSTextField:0x608000194840]   (Names: '|':NSView:0x608000128fc0 )>",
    "<NSLayoutConstraint:0x608000099410 NSTextField:0x608000194ec0.baseline == NSTextField:0x608000194f90.baseline>",
    "<NSLayoutConstraint:0x608000098ce0 NSTextField:0x608000194f90.centerY == NSTextField:0x608000194ec0.centerY>",
    "<NSLayoutConstraint:0x6080000987e0 NSTextField:0x6080001949e0.top == NSScrollView:0x6080001c6ea0.top>",
    "<NSLayoutConstraint:0x608000099730 NSTextField:0x608000194d20.baseline == NSTextField:0x608000194df0.baseline>",
    "<NSLayoutConstraint:0x608000096a30 NSTextField:0x608000194df0.centerY == NSTextField:0x608000194d20.centerY>",
    "<NSLayoutConstraint:0x608000099fa0 NSTextField:0x608000194b80.baseline == NSTextField:0x608000194c50.baseline>",
    "<NSAutoresizingMaskLayoutConstraint:0x61000068a870 h=-&- v=-&- V:[NSView:0x608000128fc0]-(0)-|   (Names: '|':NSThemeFrame:0x100350960'PDF Properties' )>",
    "<NSAutoresizingMaskLayoutConstraint:0x61000068a8c0 h=-&- v=-&- V:|-(16)-[NSView:0x608000128fc0]   (Names: '|':NSThemeFrame:0x100350960'PDF Properties' )>",
    "<NSLayoutConstraint:0x610000687d00 'NSWindow-current-height' V:[NSThemeFrame:0x100350960'PDF Properties'(532@500)] priority:500>",
    "<NSLayoutConstraint:0x608000098600 V:[NSScrollView:0x6080001c6f90]-(5)-[NSScrollView:0x6080001c6ea0]>",
    "<NSLayoutConstraint:0x608000098b50 V:[NSTextField:0x608000194f90]-(5)-[NSScrollView:0x6080001c6f90]>",
    "<NSLayoutConstraint:0x608000098e20 V:[NSTextField:0x608000194df0]-(5)-[NSTextField:0x608000194f90]>",
    "<NSLayoutConstraint:0x6080000991e0 V:[NSTextField:0x608000194c50]-(5)-[NSTextField:0x608000194df0]>"
)

Upvotes: 0

Views: 266

Answers (2)

Kalle
Kalle

Reputation: 13346

Adding a sidenote to this as it tripped me up for a long time:

If you have subviews which cannot be resized vertically, these will end up pulling the entire thing together. Example of a view which does this is the NSTextField.

For example, if you have this set up: V:|-[textField]-|, then no matter what you do, the containing view will be shrunk down to ~21 points in height.

Upvotes: 0

Ken Thomases
Ken Thomases

Reputation: 90681

Those screenshots don't convey much to me. It might be better to show the constraints in the document outline view, but really text would be best. You could add something like this to your code, someplace, and then paste the output into your question:

NSLog(@"%@", [_descriptionScrollView constraintsAffectingLayoutForOrientation:NSLayoutConstraintOrientationVertical]);

Anyway, I think what you should have are constraints like those represented by this visual format language string (more or less):

V:|-titleTextField-6-authorsScrollView(==40)-6-isbnTextField-6-publisherTextField-6-versionTextField-
6-descriptionScrollView(==keywordsScrollView,>=authorsScrollView)-6-keywordsScrollView-6-rightsTextField-
6-licenseTextField-6-licenseURLTextField-6-ccURLTextField-saveMetadataButton-|

I broke that across lines to make it easier to read.

The idea is that the vertical distance of everything except the description and keywords scroll views are fixed. The heights of the text fields are intrinsic. The height of the authors scroll view is specified by a height constraint. The distances between the controls are fixed at either the standard distance or a specified distance.

I've specified that the height of the description scroll view equals the height of the keywords scroll view. That way they should evenly divide whatever remains of the window height after all of the other required distances take their cut. I set a minimum height for the scroll views so that the window can't be reduced in height past the point where they are as tall as the authors scroll view.

Not all of the controls are mentioned in my string. The field labels and the "Copy to Clipboard" and "Show License" buttons should be set to share a baseline with the other controls on their lines. Likewise, the image view should be set relative to the license text field.

You need to avoid excess constraints beyond these. You'll over-constrain your layout and that will prevent the window from resizing (or may even result in unsatisfiable constraints). You don't have to do it exactly like I've done, but you shouldn't have more.

Also, just because I've expressed this in code doesn't mean you should do it that way. You can do this in IB. It's just easiest to explain with code-like text.

Upvotes: 1

Related Questions