在xcode中的视图上布置相等的部分

时间:2016-02-16 01:26:26

标签: ios objective-c iphone xcode interface-builder

enter image description here

我想在故事板中设置我的视图,所以它就像上面的图像一样。当水平分布50/50时,视图有2个单独的部分,然后当纵向垂直旋转到50/50时。

在xcode界面构建器中设置它的最合适方法是什么?我可以使用约束来实现这一目标吗?这将需要设计,以便它也可以扩展到ipad和iphone。

1 个答案:

答案 0 :(得分:0)

我构建了一个测试项目。它工作,检查截图

iPad中的iPad iPad in Portrait iPad在风景中 iPad in Landscape

在Storyboard中无法完成,因为iPad在Storyboard中只有一个布局。 但是以编程方式实现所需的内容非常简单。

//ViewController.m
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *firstView;
@property (nonatomic, strong) UIView *secondView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.firstView = [[UIView alloc] initWithFrame:CGRectZero];
    self.firstView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.firstView];
    self.secondView = [[UIView alloc] initWithFrame:CGRectZero];
    self.secondView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.secondView];
    [self setupSubViewLayout];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self setupSubViewLayout];
}

- (void)setupSubViewLayout {
    CGFloat viewWidth = self.view.frame.size.width;
    CGFloat viewHeight = self.view.frame.size.height;
    CGRect firstRect;
    CGRect secondRect;
    if (viewWidth > viewHeight) {
        firstRect = CGRectMake(0, 0, viewWidth/2.0, viewHeight);
        secondRect = CGRectMake(viewWidth/2.0, 0, viewWidth/2.0, viewHeight);
    }else {
        firstRect = CGRectMake(0, 0, viewWidth, viewHeight/2.0);
        secondRect = CGRectMake(0, viewHeight/2.0, viewWidth, viewHeight/2.0);
    }
    self.firstView.frame = firstRect;
    self.secondView.frame = secondRect;
}