关于self.view.window与viewDidLoad、viewWillAppear、viewDidAppear
在进入一个界面(UIViewController),如果要在进入的时候使用self.view.window,那么必须在将使用代码放在viewDidAppear方法中,而viewDidLoad、viewWillAppear中self.view.window.frame为0{{0,0},{0,0}}。
- (void)viewDidLoad {
NSLog(@"viewDidLoad");
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
NSLog(@"viewWillAppear");
[super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear");
[super viewDidAppear:animated];
[self _doTest01];
}
- (void)_doTest01 {
NSLog(@"_doTest01");
//UIView
UIView* vwFullScreenView = [[UIView alloc]init];
vwFullScreenView.backgroundColor=[UIColor yellowColor];
vwFullScreenView.frame=self.view.window.frame;
NSLog(@"%@",NSStringFromCGRect(vwFullScreenView.frame));
[self.view.window addSubview:vwFullScreenView];
//UILabel
UILabel* lblTitle = [[UILabel alloc]init];
lblTitle.text=@"显示了@_@";
lblTitle.frame=CGRectMake(0, 0, 200, 40);
lblTitle.textAlignment=NSTextAlignmentCenter;
lblTitle.backgroundColor=[UIColor whiteColor];
lblTitle.center=vwFullScreenView.center;
[vwFullScreenView addSubview:lblTitle];
}
输出:
2015-11-01 09:21:27.031 demo010[7638:261914] viewDidLoad
2015-11-01 09:21:27.032 demo010[7638:261914] viewWillAppear
2015-11-01 09:21:27.038 demo010[7638:261914] viewDidAppear
2015-11-01 09:21:27.038 demo010[7638:261914] _doTest01
2015-11-01 09:21:27.039 demo010[7638:261914] {{0, 0}, {320, 568}}
如果将[self _doTest01];代码放到viewDidLoad 或 viewWillAppear 方法中,那么输出结果如下:
2015-11-01 09:35:00.054 demo010[7762:271018] viewDidLoad
2015-11-01 09:35:00.054 demo010[7762:271018] viewWillAppear
2015-11-01 09:35:00.054 demo010[7762:271018] _doTest01
2015-11-01 09:35:00.054 demo010[7762:271018] {{0, 0}, {0, 0}}
2015-11-01 09:35:00.061 demo010[7762:271018] viewDidAppear
可以看到self.view.window的frame为 {{0, 0}, {0, 0}},说明此时self.view.window还没显示出来。
标签:11,01,viewWillAppear,self,09,window,demo010 From: https://blog.51cto.com/u_16160131/6473907