これは機能します (プロジェクトの Dropbox 共有):
秘訣は、目に見えない UIView をスペーサー オブジェクトとして使用することです。animView と呼ぶビューは、このように設定されています。これは IB で部分的に設定したため、まだいくつかの問題が発生しますが、希望どおりに動作することがわかります。
@implementation AnimView {
UIView * _innerBox;
UIView * _spacer;
}
次に、または- setUpEverything
からメソッドを呼び出します。initWithCoder:
initWithFrame:
- (void) setUpEverything {
// Because I set this up in IB with AutoLayout off, I think it
// still needs this. Won't work without it
self.translatesAutoresizingMaskIntoConstraints = YES;
// Otherwise you will see the box outside of animView
self.clipsToBounds = YES;
_innerBox = [[UIView alloc] init];
_innerBox.backgroundColor = [UIColor blackColor];
_spacer = [[UIView alloc] init];
_spacer.backgroundColor = [UIColor clearColor];
_innerBox.translatesAutoresizingMaskIntoConstraints = NO;
_spacer.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_innerBox];
[self addSubview:_spacer];
// Give _innerBox and _spacer some dimensions to prevent ambiguity
NSLayoutConstraint *i02 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
NSLayoutConstraint *i03 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
NSLayoutConstraint *s02 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
// Center both views
NSLayoutConstraint *i05 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *s04 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
// Set the top of _innerBox 10.0 points from the top of the superview with a low priority
NSLayoutConstraint *i01 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f];
i01.priority = 250;
// Pin the spacer to the bottom of _innerBox;
NSLayoutConstraint *i04 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_spacer attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
// Pin the spacer's bottom to the bottom of the superview
NSLayoutConstraint *s01 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
// Stretch the spacer out as needed
NSLayoutConstraint *s03 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:1.0f constant:80.0f];
[self addConstraints:@[i01,i02,i03,i04,i05,s01,s02,s03,s04]];
}