Pregunta

I have some issue with autolayout.

We have ViewController with root view and child view. Child view have fixed aspect ratio. I need to fit child view in parent while rotation. Also chid view should be centered. Like on a picture: aspect fit demonstration

I have this code:

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

}

And it's not working, it's shrinks outside superview in landscape.

¿Fue útil?

Solución

I've made it work with the following constraints:

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

Screenshots:

enter image description here

enter image description here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top