2

我对自动布局有一些问题。

我们有带有根视图和子视图的 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];

}

而且它不起作用,它缩小了景观的超级视野。

4

1 回答 1

6

我已经使它在以下约束条件下工作:

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

截图:

在此处输入图像描述

在此处输入图像描述

于 2013-10-22T14:40:16.557 回答