2013-08-14 38 views
6

Tôi muốn hình ảnh con của tôi là hình chữ nhật 16: 9 ở trên cùng của trình giám sát. Nói cách khác, tôi muốn nó:Mã NSLayoutConstraints để căn giữa chế độ xem và duy trì tỷ lệ khung hình

  1. được rộng như nó SuperView, nhưng không lớn hơn 400px (UI có thể xoay theo chiều ngang),
  2. được tập trung theo chiều ngang khi nó hẹp hơn đó là SuperView,
  3. có phần trên cùng được gắn vào đầu của người giám sát và
  4. thay đổi chiều cao của nó để duy trì tỷ lệ khung hình 16: 9.

Mã này gần như không nó, ngoại trừ tôi đang gặp một thời gian khó khăn làm hạn chế ngang làm việc và không có trên hoặc dưới chế ...

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

Trả lời

14

Nếu bạn muốn tập trung vào hộp màu đỏ theo chiều ngang , sau đó bạn muốn cân bằng giữa các chế độ xem 'CenterX, chứ không phải CenterY. Vì vậy, hạn chế đó sẽ giống như sau:

[NSLayoutConstraint constraintWithItem:contentView 
          attribute:NSLayoutAttributeCenterX 
          relatedBy:NSLayoutRelationEqual 
           toItem:self.view 
          attribute:NSLayoutAttributeCenterX 
          multiplier:1.f constant:0.f]]; 

Sau đó, trên những hạn chế đầu tiên, những ràng buộc bạn phải có mơ hồ kể từ khi có nhiều hơn một cách để đáp ứng >=0 lề mỗi bên và <=400 rộng. Một cách tốt hơn là nói chính xác những gì bạn đã nói trong câu hỏi của mình, đó là bạn cần chiều rộng là < = 400 và bạn sẽ như lợi nhuận là 0 nếu có thể.

Vì vậy, một cái gì đó như thế này:

[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-([email protected])-[contentView(<=400)]-([email protected])-|" 
             options:0 metrics:0 views:views]]; 

Tôi tin rằng được bạn muốn, bạn muốn?

+0

Chắc chắn là vậy. Câu trả lời rất hữu ích. Cảm ơn. – danh

0

Đôi khi, tôi nghĩ có giải pháp khác, tôi đã sao chép giải pháp của mình tại đây.

Nếu bạn muốn ngang Alignment, chỉ cần sử dụng nó [parentView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

Nếu bạn muốn Vertical Alignment, chỉ cần sử dụng nó [parentView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];

Và giải pháp này làm việc cho tôi, tôi hy vọng rằng tôi chia sẻ nó với một người nào khác.

Các vấn đề liên quan