简体   繁体   中英

Resize NSWindow with “semi-fixed” aspect ratio

(I hope the tile makes sense...) I'm trying to make my NSWindow resizing be "constrained" by it's content, which is a view with a fixed aspect ratio, and a spacing at the bottom, like in the picture.

截图

The constraints are, from ContextBackedView to superview, leading, trailing and top space equals to 0, and bottom space equals to 37, plus the aspect ratio constraint (418:263)

Here's the output for constraintsAffectingLayoutForOrientation (0x6000001217c0 is the window contentView) NSLayoutConstraintOrientationHorizontal‌

<NSAutoresizingMaskLayoutConstraint:0x618000080af0 h=-&- v=-&- H:|-(0)-[NSView:0x6000001217c0]   (Names: '|':NSThemeFrame:0x1003060c0'Window' )>,
<NSAutoresizingMaskLayoutConstraint:0x618000080b40 h=-&- v=-&- H:[NSView:0x6000001217c0]-(0)-|   (Names: '|':NSThemeFrame:0x1003060c0'Window' )>,
<NSLayoutConstraint:0x618000080870 'NSWindow-current-width' H:[NSThemeFrame:0x1003060c0'Window'(418@500)] priority:500>

NSLayoutConstraintOrientationVertical

<NSAutoresizingMaskLayoutConstraint:0x618000080af0 h=-&- v=-&- H:|-(0)-[NSView:0x6000001217c0]   (Names: '|':NSThemeFrame:0x1003060c0'Window' )>,
<NSAutoresizingMaskLayoutConstraint:0x618000080b40 h=-&- v=-&- H:[NSView:0x6000001217c0]-(0)-|   (Names: '|':NSThemeFrame:0x1003060c0'Window' )>,
<NSLayoutConstraint:0x6000000828a0 H:[NSView:0x600000121860]-(0)-|   (Names: '|':NSView:0x6000001217c0 )>,
<NSLayoutConstraint:0x600000082990 H:|-(0)-[NSView:0x600000121860]   (Names: '|':NSView:0x6000001217c0 )>,
<NSLayoutConstraint:0x618000080870 'NSWindow-current-width' H:[NSThemeFrame:0x1003060c0'Window'(418@500)] priority:500>,
<NSAutoresizingMaskLayoutConstraint:0x618000080b90 h=-&- v=-&- V:[NSView:0x6000001217c0]-(0)-|   (Names: '|':NSThemeFrame:0x1003060c0'Window' )>,
<NSLayoutConstraint:0x6000000828f0 V:|-(0)-[NSView:0x600000121860]   (Names: '|':NSView:0x6000001217c0 )>,
<NSLayoutConstraint:0x600000082940 V:[NSView:0x600000121860]-(37)-|   (Names: '|':NSView:0x6000001217c0 )>,
<NSLayoutConstraint:0x6000000800f0 NSView:0x600000121860.width == 1.58935*NSView:0x600000121860.height>

Using auto layout, it only works if I resized the window's width, but not if I try to change it's height. I can't use -[NSWindow setAspectRatio:] since the ratio of the window is not actually a fixed number (due to the bottom space having to stay the same)

I also tried using -windowWillResize:toSize: but I couldn't find a way to make it work reliably (resizing only width or height is fine but when changing both it usually broke...)

Does anyone knows a way to achieve this?

If you add a constraint (Aspect Ration Constraint) to your ContextBackedView , with the multiplier property being the aspect ratio you'd like (eg 4:3), you should be able to achieve what you want.

It's easy if you are building this through IB-- first select your ContextBackedView , and on the bottom-right click on the box with lines icon, and select "Aspect Ratio". That will maintain your view's current aspect ratio after being resized.

If you want to do it programmatically you'll have to look into instantiating and applying NSLayoutConstraints onto NSViews . I have never done it with Aspect Ratio Constraints, but it should be pretty straight forward. If you want a code sample let me know I can spin one up :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM