实现具有固定宽高比的纵横比适合的子UIView

时间:2013-10-22 12:02:37

标签: ios objective-c uiview autolayout aspect-ratio

我在autolayout方面遇到了一些问题。

我们有ViewController,它有根视图和子视图。 子视图具有固定的宽高比。 我需要在旋转时将父视图放在父视图中。 chid视图也应该居中。 就像在图片上: aspect fit demonstration

我有这段代码:

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

}

而且它不起作用,它在风景中的超级视图之外缩小了。

1 个答案:

答案 0 :(得分: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];
}

截图:

enter image description here

enter image description here