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- bu Superview olandan daha dar olduğunda
- yatay merkezlenebilir, geniş olması,
- , üst tarafın üst kısmına sabitlenmiş ve
- 'un 16: 9 en boy oranını korumak için yüksekliğini değiştirmesini sağlayın. 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];
}
Kesinlikle öyle. Çok kullanışlı bir cevap. Teşekkürler. – danh