简体   繁体   English

NSView约束-如何强制子视图的纵横比保持恒定?

[英]NSView Contraints - How can I force the aspect ratio of a subview to stay constant?

Having trouble implementing the following: 无法执行以下操作:

+-------------------+
|                   |
|                   |
|     RESIZABLE     |
|                   |
|      NSVIEW       |
|                   |
|                   |
|                   |
|                   |
|                   |
|                   |
|                   |
|                   |
|                   |
|+-----------------+|
||                 ||
||     STATIC      ||
||                 ||
||     SUBVIEW     ||
||                 ||
|+-----------------+|
+-------------------+

Where the aspect ratio of the subview must stay constant (as the user changes the width of the resizeable view). 子视图的纵横比必须保持恒定的位置(随着用户更改可调整大小的视图的宽度)。

I'd like to do it using constraints, so far I've got: 我想使用约束来做到这一点,到目前为止,我已经:

//This is a snippet of the resizable view class

[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

//This is a snippet of the subview class

[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:aspectRatio constant:0]];

The frame of the resizable view is initially set. 最初设置了可调整大小视图的框架。 In this case it would make sense not to set the frame of the subview? 在这种情况下,不设置子视图的框架是否有意义? At the moment, the main view just seems to disappear - it starts width a width of zero. 此刻,主视图似乎消失了-它的宽度从零开始。

How should I go about this - I'd love to use constraints if possible! 我应该如何处理-如果可能,我很乐意使用约束!

This can be done by setting up most of the constraints in IB, then changing one of them in code. 这可以通过在IB中设置大多数约束,然后在代码中更改其中之一来完成。 The static subview should have whatever spacing constraints you want to the sides and bottom of the resizable view, and a fixed height constraint -- after making those, you should be able to delete the constraint to the top of the resizable view if there is one. 静态子视图在可调整大小的视图的侧面和底部应具有所需的任何间距限制,以及固定的高度限制-制作完这些高度限制后,如果有一个约束,则应该可以将其删除到可调整大小的视图顶部的约束。 Make an IBOutlet to the height constraint, and change that in code like this (heightCon is my outlet, and this code is in the static subview subclass): 将IBOutlet设置为高度限制,然后在这样的代码中进行更改(heightCon是我的出口,并且此代码在静态子视图子类中):

- (void)awakeFromNib {
    [self removeConstraint:self.heightCon];
    self.heightCon = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:.54 constant:0];
    [self addConstraint:self.heightCon];
}

The .54 number came from my initial ratio of height to width in IB. .54的数字来自我最初在IB中的高度与宽度之比。

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

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