【发布时间】:2015-11-09 11:03:53
【问题描述】:
根据设备方向,autolayout 约束有不同的值。我以这种方式更新约束:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
self.backgroundView.image = [UIImage imageNamed:@"landscape.jpg"];
[self updateLandscapeConstraints];
}
else if ((orientation == UIInterfaceOrientationLandscapeLeft) ||
(orientation == UIInterfaceOrientationLandscapeRight)) {
self.backgroundView.image = [UIImage imageNamed:@"portrait.jpg"];
[self updatePortraitConstraints];
}
}
在一些帖子中,我看到[self.view setNeedsUpdateConstraints] 在应用约束更新后被调用,而在其他帖子中,[self.view layoutIfNeeded] 被调用。有什么区别?
提前致谢
编辑:我这样更新约束,对吗?:
- (void)updateLandscapeConstraints
{
[self.view layoutIfNeeded];
self.passwordViewHeight.constant = 34.0;
self.usernameViewHeight.constant = 34.0;
[self.view removeConstraint:self.registrationButtonEqualWidth];
self.registrationButtonEqualWidth = [NSLayoutConstraint constraintWithItem:self.registrationButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.backgroundView
attribute:NSLayoutAttributeWidth
multiplier:0.6
constant:0.0];
[self.view addConstraint:self.registrationButtonEqualWidth];
[self.view layoutIfNeeded];
}
【问题讨论】:
标签: ios rotation autolayout orientation nslayoutconstraint