答案 0 :(得分:2)
在故事板中设置约束以获得纵向方向所需的布局。然后添加两个额外的约束并将其优先级从1000更改为999:
为以下约束创建IBOutlet
:
例如:
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomSpaceToViewConstraint;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leadingSpaceToSuperviewConstraint;
从weak
转换为strong
引用非常重要,以便将约束保留在内存中,即使它们不活动也是如此。
最后实现以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
// check if viewcontroller is opened in landscape
CGSize size = self.view.bounds.size;
if (size.width > size.height) {
// is landscape
self.bottomSpaceToViewConstraint.active = NO;
self.leadingSpaceToSuperviewConstraint.active = NO;
}
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
if (size.height > size.width) {
// will transition to portrait
self.bottomSpaceToViewConstraint.active = YES;
self.leadingSpaceToSuperviewConstraint.active = YES;
} else {
// will transition to landscape
self.bottomSpaceToViewConstraint.active = NO;
self.leadingSpaceToSuperviewConstraint.active = NO;
}
}
随时询问是否有不清楚的事情!