ayoutSubviews的调用时机:
2.将要真正显示的会调用
3.frame发现改变时智能调用
4.滚动、旋转、remove等等时
这些时机都是和frame相关的,也是唯一能更新子视图的最好时机
layoutSubviews的调用时机:http://blog.youkuaiyun.com/zhangao0086/article/details/6904854
drawRect是在视图被真正显示的时候,如果layer的contents没有内容,就会被调用
横竖屏demo:
A:----views
@interface DetialViews ()<UIWebViewDelegate>{
UIView *bgView;
UIWebView *webView;
}
@end
@implementation DetialViews
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview{
if (newSuperview !=nil) {
if ([self respondsToSelector:@selector(setupViews)]) {
[self setupViews];
}
if ([self respondsToSelector:@selector(addSubviews)]) {
[self addSubviews];
}
}
}
- (void)layoutSubviews{
[super layoutSubviews];
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationPortrait:{
if ( [self respondsToSelector:@selector(layoutPortrait)]) {
[self layoutPortrait];
}
}
break;
case UIInterfaceOrientationLandscapeRight:
{
if ([self respondsToSelector:@selector(layoutLandscape)]) {
[self layoutLandscape];
}
}
break;
case UIInterfaceOrientationPortraitUpsideDown:
default:
break;
}
}
- (void)setupViews{
bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor blueColor];
webView = [[UIWebView alloc] init];
webView.backgroundColor = [UIColor whiteColor];
NSURL *url =[NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
- (void)addSubviews{
[self addSubview:bgView];
[self addSubview:webView];
}
- (void)layoutLandscape{
[bgView setFrame:CGRectMake(0, 0, TjScreeenWeight, 60)];
[webView setFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, self.frame.size.width, TjScreenHeight - bgView.frame.size.height )];
NSLog(@"home右边----->%f---%f",bgView.bounds.size.width,bgView.bounds.size.height);
}
- (void)layoutPortrait{
[bgView setFrame:CGRectMake(0, 0, TjScreeenWeight, 60)];
[webView setFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, self.frame.size.width, TjScreenHeight - bgView.frame.size.height )];
NSLog(@"home下边----->%f---%f",bgView.bounds.size.width,bgView.bounds.size.height);
}
B: viewController
#import "DetialVViewController.h"
#import "DetialViews.h"
@interface DetialVViewController (){
DetialViews * _detialView;
}
@end
@implementation DetialVViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViews];
}
- (void)addSubViews{
_detialView = [[DetialViews alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_detialView];
}