Iphone NSLayoutConstraints用于将视图居中并保持其纵横比的代码

Iphone NSLayoutConstraints用于将视图居中并保持其纵横比的代码,iphone,ios,autolayout,Iphone,Ios,Autolayout,我希望我的子视图是一个以超级视图顶部为中心的16:9矩形。换句话说,我希望它: 与superview一样宽,但不超过400px(UI可以旋转到横向) 当它比superview窄时,水平居中 将其顶部固定到superview的顶部,然后 更改其高度以保持16:9的纵横比 这段代码几乎可以做到这一点,除了我很难使水平约束工作,而且不能过度或不足约束 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setu

我希望我的子视图是一个以超级视图顶部为中心的16:9矩形。换句话说,我希望它:

  • 与superview一样宽,但不超过400px(UI可以旋转到横向)
  • 当它比superview窄时,水平居中
  • 将其顶部固定到superview的顶部,然后
  • 更改其高度以保持16:9的纵横比
  • 这段代码几乎可以做到这一点,除了我很难使水平约束工作,而且不能过度或不足约束

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UIView *contentView = [[UIView alloc] init];
        contentView.backgroundColor = [UIColor redColor];
        [self.view addSubview:contentView];
        
        contentView.translatesAutoresizingMaskIntoConstraints = NO;
        
        NSDictionary *views = NSDictionaryOfVariableBindings(contentView);
        NSMutableArray *constraints = [NSMutableArray array];
    
        // this layout string is more like 'wishful coding'.  I don't see why it wouldn't work
        // but clearly this one is the problem
        [constraints addObjectsFromArray:[NSLayoutConstraint
                                          constraintsWithVisualFormat:@"H:|-(>=0)-[contentView(<=400)-(>=0)-]"
                                          options:0 metrics:0 views:views]];
        
        // this centering constraint below almost does the job, but doesn't give me a way
        // to specify width, changing the one above to just @"H:[contentView(<=400)]"
        // doesn't work either
        [constraints addObject:
         [NSLayoutConstraint constraintWithItem:contentView
                                      attribute:NSLayoutAttributeCenterY
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:self.view
                                      attribute:NSLayoutAttributeCenterY
                                     multiplier:1.f constant:0.f]];
    
        // 9:16 works fine, I think
        [constraints addObject:
         [NSLayoutConstraint constraintWithItem:contentView
                                      attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:contentView attribute:NSLayoutAttributeWidth
                                     multiplier:9.0/16.0 constant:0.0]];
        
        // pin the tops works fine, I think
        [constraints addObjectsFromArray:
         [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[contentView]"
                                                 options:0 metrics:0 views:views]];
    
        [self.view addConstraints:constraints];
    }
    
    -(void)viewDidLoad
    {
    [超级视图下载];
    //加载视图后执行任何其他设置。
    UIView*contentView=[[UIView alloc]init];
    contentView.backgroundColor=[UIColor redColor];
    [self.view addSubview:contentView];
    contentView.translatesAutoResizezingMaskintoConstraints=否;
    NSDictionary*视图=NSDictionaryOfVariableBindings(contentView);
    NSMutableArray*约束=[NSMutableArray];
    //这个布局字符串更像是“一厢情愿的编码”。我不明白为什么它不起作用
    //但很明显,这就是问题所在
    [constraints addObjectsFromArray:[NSLayoutConstraint]
    具有VisualFormat的约束:@“H:|-(>=0)-[contentView(=0)-]
    选项:0指标:0视图:视图]];
    //下面的定心约束几乎可以完成这项工作,但没有给我一个方法
    
    //若要指定宽度,请将上面的一个更改为仅@“H:[contentView(如果要将红色框水平居中,则要将视图的CenterX而不是CenterY相等。因此,该约束应如下所示:

    [NSLayoutConstraint constraintWithItem:contentView
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeCenterX
                                multiplier:1.f constant:0.f]];
    
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0@900)-[contentView(<=400)]-(0@900)-|"
                                            options:0 metrics:0 views:views]];
    

    然后在第一个约束上,您的约束是不明确的,因为有不止一种方法可以满足两边的
    =0
    边距和
    有时,我认为还有另一种解决方案,我在这里复制了我的解决方案

    如果你想水平对齐,就用它
    
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:子视图属性:NSLayoutAttributeCenter relatedBy:NSLayoutRelationEqual toItem:父视图属性:NSLayoutAttributeCenter乘数:1常量:0];
    

    如果您想垂直对齐,只需使用它
    
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:子视图属性:NSLayoutAttributeCenter relatedBy:NSLayoutRelationEqual toItem:父视图属性:NSLayoutAttributeCenter乘数:1常量:0];
    


    这个解决方案对我很有效,我希望我能把它分享给其他人。

    当然可以。非常有用的答案。谢谢。