可以通过代码轻松实现所需的 UI。
首先在 viewDidLoad 方法中以编程方式添加三个视图。在 viewDidLoad 中,我们将根据方向应用约束。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_constraints = [NSMutableArray array];
_view1 = [[UIView alloc] init];
_view1.translatesAutoresizingMaskIntoConstraints = NO;
_view1.backgroundColor = [UIColor greenColor];
[self.view addSubview:_view1];
_view2 = [[UIView alloc] init];
_view2.translatesAutoresizingMaskIntoConstraints = NO;
_view2.backgroundColor = [UIColor redColor];
[self.view addSubview:_view2];
_view3 = [[UIView alloc] init];
_view3.translatesAutoresizingMaskIntoConstraints = NO;
_view3.backgroundColor = [UIColor blueColor];
[self.view addSubview:_view3];
switch ([self.view viewOrientation]) {
case ViewOrientationPortrait:
[self setConstraintsForPortrait];
break;
case ViewOrientationLandscape:
[self setConstraintsForLandScape];
break;
default:
break;
}
}
此方法为横向模式添加约束。它首先删除了我们可能在纵向模式中添加的所有先前添加的约束。
- (void)setConstraintsForLandScape {
for (id c in _constraints) {
[self.view removeConstraints:c];
}
[_constraints removeAllObjects];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_view1][_view2(==_view1)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view1, _view2)]];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_view3]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view3)]];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_view1][_view3(==_view1)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view1, _view3)]];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_view2(==_view1)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view1, _view2)]];
for (id c in _constraints) {
[self.view addConstraints:c];
}
}
这里我们再次为纵向模式添加约束,首先删除任何先前添加的约束。
- (void)setConstraintsForPortrait {
for (id c in _constraints) {
[self.view removeConstraints:c];
}
[_constraints removeAllObjects];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_view1]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view1)]];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_view2]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view2)]];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_view3]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view3)]];
[_constraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_view1][_view2(==_view1)][_view3(==_view1)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view1, _view2, _view3)]];
for (id c in _constraints) {
[self.view addConstraints:c];
}
}
此方法将处理方向转换
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
// Code here will execute before the rotation begins.
// Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]
NSLog(@"%@", NSStringFromCGSize(size));
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Place code here to perform animations during the rotation.
// You can pass nil or leave this block empty if not necessary.
switch ([self.view viewOrientation]) {
case ViewOrientationPortrait:
[self setConstraintsForPortrait];
break;
case ViewOrientationLandscape:
[self setConstraintsForLandScape];
break;
default:
break;
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Code here will execute after the rotation has finished.
// Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:]
}];
}