2

自動レイアウトに問題があります。

ルート ビューと子ビューを持つ ViewController があります。子ビューの縦横比は固定です。回転中に子ビューを親に合わせる必要があります。また、chid ビューを中央に配置する必要があります。写真のように: アスペクト フィットのデモンストレーション

私はこのコードを持っています:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView * v = [UIView new];
    v.backgroundColor = [UIColor redColor];
    v.translatesAutoresizingMaskIntoConstraints = NO;
    v.tag = 100;
    [self.view addSubview:v];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:@{@"v":v}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:@{@"v":v}]];
    for (NSLayoutConstraint *c in self.view.constraints) {
        [c setPriority:800];
    }

    NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:v
                                                         attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:v
                                                         attribute:NSLayoutAttributeWidth
                                                        multiplier:0.8
                                                          constant:0.];
    [c setPriority:1000];
    [v addConstraint:c];


    c = [NSLayoutConstraint constraintWithItem:v
                                     attribute:NSLayoutAttributeCenterY
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeCenterY
                                    multiplier:1
                                      constant:0.];
    [c setPriority:1000];
    [self.view addConstraint:c];

    c = [NSLayoutConstraint constraintWithItem:v
                                     attribute:NSLayoutAttributeCenterX
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeCenterX
                                    multiplier:1
                                      constant:0.];
    [c setPriority:1000];
    [self.view addConstraint:c];

}

そして、それは機能していません。ランドスケープのスーパービューの外で縮小しています。

4

1 に答える 1