我对自动排版有一些问题。

我们有带根视图和子视图的ViewController。
子视图具有固定的宽高比。
我需要在旋转时将子视图适合父视图。
顶视图也应居中。
就像在图片上一样:

我有这个代码:

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

}

而且它不起作用,它缩小了外部 super 视图的范围。

最佳答案

我使其具有以下约束条件:

- (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:|-(>=0)-[v]-(>=0)-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:@{@"v":v}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[v]-(>=0)-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:@{@"v":v}]];

    NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:v
                                                         attribute:NSLayoutAttributeWidth
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeWidth
                                                        multiplier:1.0
                                                          constant:0];
    [c setPriority:800];
    [self.view addConstraint:c];

    c = [NSLayoutConstraint constraintWithItem:v
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeHeight
                                    multiplier:1.0
                                      constant:0];
    [c setPriority:800];
    [self.view addConstraint:c];

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

屏幕截图:

关于ios - 以固定的宽高比实现宽高比适合子UIView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19517330/

10-12 02:09