iOS Xib尺寸适配屏幕尺寸

有用过XIb 的同学 在自己设置 VC 下的子控件的Frame时 可能会发现 在ViewDidload 下面 获取 的 self.view.frame.size.height 是Xib的尺寸 非当前VC的尺寸,这就存在了Xib 的尺寸 和 屏幕的尺寸 不匹配的情况。

这时 我们只需要 在   viewWillLayoutSubviews 获取 VC的frame即可。另外 在有XIb 的 View 下面可通过调用 layoutSubviews 方法 来重设 子控件 frame。

示例代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"the view height is %f", self.view.frame.size.height);
    
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    
    NSLog(@"the view height is %f", self.view.frame.size.height);
    
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    NSLog(@"the view height is %f", self.view.frame.size.height);
    
}

- (void)layoutSublayersOfLayer:(CALayer *)layer{
    
    NSLog(@"the view height is %f", self.view.frame.size.height);
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSLog(@"the view height is %f", self.view.frame.size.height);
    
}

- (void)viewWillLayoutSubviews{
    
    NSLog(@"the view height is %f", self.view.frame.size.height);
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"the view height is %f", self.view.frame.size.height);
    
    PlayViewController *palyVC = [PlayViewController new];
    [self.navigationController pushViewController:palyVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


你可能感兴趣的:(iOS_UI)