2013-08-14 5 views
6

Alt sayfamın, süper görüntünün en üstünde bulunan 16: 9 dikdörtgen olmasını isterim. Diğer bir deyişle, o istiyorum:NSLayoutGörüntüyü ortalamak ve en boy oranını korumak için kodu

bu Superview, ama herhangi bir geniş 400 piksel daha (UI manzara dönebilir) halinde
  1. bu Superview olandan daha dar olduğunda
  2. yatay merkezlenebilir, geniş olması,
  3. , üst tarafın üst kısmına sabitlenmiş ve
  4. 'un 16: 9 en boy oranını korumak için yüksekliğini değiştirmesini sağlayın.
  5. Ben zor bir zaman yatay kısıtlamalar üzerinden veya kısıtlı altında çalışmak değil, yapım yaşıyorum hariç Bu kod neredeyse yapar

...

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

cevap

14

yatay kırmızı kutu ortalamak isterseniz o zaman, CenterY değil 'CenterX' görüntülerini eşitlemek istersiniz. O kısıtlama gibi görünmelidir Yani:

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

Sonra ilk kısıtlamaları üzerinde >=0 her iki tarafta marjı ve genişliği <=400 karşılamak için birden fazla yolu olduğundan, sahip kısıtları belirsiz vardır. genişliğinin < = 400 olması gerektiği ve gibi olmasını istiyorsanız, mümkünse 0 olması gereken daha iyi bir yol olacaktır.

Yani böyle bir şey:

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

Bunu istediğiniz istiyor alır inanıyoruz?

+0

Kesinlikle öyle. Çok kullanışlı bir cevap. Teşekkürler. – danh

0

Bazen bence başka bir çözüm var, çözümümü buraya kopyaladım.

Eğer Yatay Hizalama istiyorsanız, sadece Dikey Hizalama istiyorsanız o [parentView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

, sadece [parentView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];

kullanmak kullanmak Ve bu çözüm benim için çalışıyor, ben bunu paylaşmak umut başkası.