计算视图相对坐标时convertPoint:toView: ,UIApplication sharedApplication - keyWindow is nil?

   UIWindow *window = [UIApplication sharedApplication].keyWindow;

 

 window 为nil的原因:在指定rootViewController之前,没有让window 先可视化

 

1.示例

 为nil:

 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

   self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController = [[ViewController alloc] init];

 

   [self.window makeKeyAndVisible]; 

 

 不为nil:

   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

 

   [self.window makeKeyAndVisible];

 

   self.window.backgroundColor = [UIColor whiteColor];

   self.window.rootViewController = [[ViewController alloc] init];

 

2.为nil的解决方法二

 用 delegate方法即可

 UIWindow *window = [[UIApplication sharedApplication].delegate window];

 

posted @ 2016-04-21 01:41  会飞的夜良  阅读(834)  评论(0编辑  收藏  举报