简体   繁体   English

使用“半固定”宽高比调整NSWindow的大小

[英]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. (我希望该图块有意义。)我正在尝试使NSWindow的大小受其内容“约束”,该内容是具有固定长宽比的视图,底部具有一个间距,如图中所示。

截图

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) 从ContextBackedView到superview的约束是,前导,尾随和顶部空间等于0,底部空间等于37,加上长宽比约束(418:263)

Here's the output for constraintsAffectingLayoutForOrientation (0x6000001217c0 is the window contentView) NSLayoutConstraintOrientationHorizontal‌ 这是constraintsAffectingLayoutForOrientation的输出(0x6000001217c0是窗口contentView)NSLayoutConstraintOrientationHorizo​​ntal‌

<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 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) 我不能使用-[NSWindow setAspectRatio:],因为窗口的比例实际上不是固定数字(由于底部空间必须保持不变)

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...) 我也尝试使用-windowWillResize:toSize:但我找不到一种方法来使其可靠地工作(仅调整宽度或高度的大小是可以的,但同时更改两者通常会损坏...)

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. 如果您向ContextBackedView添加约束(纵横比约束),并且multiplier属性是您想要的纵横比(例如4:3),则您应该能够实现所需的目标。

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". 如果您是通过IB构建的,则很容易-首先选择ContextBackedView ,然后在右下角单击带有线条图标的框,然后选择“纵横比”。 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 . 如果要以编程方式执行此操作,则必须考虑实例化NSLayoutConstraints并将其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 :) 如果您想要一个代码示例,请让我知道我可以升级:)

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

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