本文介绍了NSView Contraints - 如何强制子视图的宽高比保持不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法实施以下操作:

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


$ b b

其中子视图的宽高比必须保持不变(用户更改可调整大小的视图的宽度)。

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.

我应该怎么走 - 我想使用约束if可能的!

How should I go about this - I'd love to use constraints if possible!

推荐答案

这可以通过在IB中设置大多数约束,然后在代码中更改其中的一个来完成。静态子视图应该有任何空间约束你想要的可调整大小视图的侧面和底部,和一个固定的高度约束 - 在做出这些后,你应该能够删除约束到可调整大小的视图的顶部,如果有一个。将一个IBOutlet设置为高度约束,并在代码中这样改变(heightCon是我的出口,这个代码在静态子视图子类):

This can be done by setting up most of the constraints in IB, then changing one of them in code. 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):

- (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];
}

.54数字来自我在IB中的初始高度比宽度。

The .54 number came from my initial ratio of height to width in IB.

这篇关于NSView Contraints - 如何强制子视图的宽高比保持不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:14