对于这种情况,我建议通过代码以编程方式进行布局。
这是示例:
https://dl.dropboxusercontent.com/u/48223929/Test.zip
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self layoutImageViewsForViewSize:self.view.frame.size];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[self layoutImageViewsForViewSize:size];
}
- (void)layoutImageViewsForViewSize:(CGSize)size
{
// Portrait mode
if (size.height > size.width) {
self.imageView1.frame = CGRectMake(0, 0, size.width, size.height / 2);
self.imageView2.frame = CGRectMake(0, size.height / 2, size.width, size.height / 2);
// Landscape mode
} else {
self.imageView1.frame = CGRectMake(0, 0, size.width / 2, size.height);
self.imageView2.frame = CGRectMake(size.width / 2, 0, size.width / 2, size.height);
}
}
对于自动布局:
- (void)layoutImageViewsForViewSize:(CGSize)size
{
// Portrait mode
[self.view removeConstraints:self.view.constraints];
if (size.height > size.width) {
NSString *heightLayoutFormatString = [NSString stringWithFormat:@"V:|[_imageView1(%.0f)][_imageView2(%.0f)]|", size.height / 2, size.height / 2];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightLayoutFormatString options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1, _imageView2)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|[_imageView1(%.0f)]|", size.width] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|[_imageView2(%.0f)]|", size.width] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView2)]];
// Landscape mode
} else {
NSString *widthLayoutFormatString = [NSString stringWithFormat:@"H:|[_imageView1(%.0f)][_imageView2(%.0f)]|", size.width / 2, size.width / 2];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthLayoutFormatString options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1, _imageView2)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_imageView1(%.0f)]|", size.height] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_imageView2(%.0f)]|", size.height] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView2)]];
}
[self.view setNeedsUpdateConstraints];
}
自动布局的另一种解决方案 - 制作包含 2 个图像视图的 UIView 的自定义子类,并在没有约束的情况下以编程方式对其进行布局 - 只需 setFrame。
然后将此自定义 UIView 添加到您的故事板