0

次の実装に問題があります。

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

サブビューの縦横比を一定に保つ必要がある場所 (ユーザーがサイズ変更可能なビューの幅を変更するため)。

これまでのところ、制約を使用してそれを行いたいと思います:

//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]];

サイズ変更可能なビューのフレームは、最初に設定されています。この場合、サブビューのフレームを設定しないのは理にかなっていますか? 現時点では、メイン ビューが消えているように見えます。最初は幅 0 です。

どうすればいいですか - 可能であれば、制約を使用したいと思います!

4

1 に答える 1

3

これは、IB でほとんどの制約を設定し、そのうちの 1 つをコードで変更することで実行できます。静的サブビューには、サイズ変更可能なビューの側面と下部に必要な間隔の制約と、固定の高さの制約が必要です。これらを作成した後、サイズ変更可能なビューの上部にある制約がある場合は、その制約を削除できるはずです。 . 高さの制約に 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];
}

.54 という数字は、IB の最初の高さと幅の比率から来ています。

于 2013-05-13T15:57:37.867 に答える