// // AppDelegate.m // 09-控制器View懒加载 #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; // 创建UIViewController控制器,控制器的view并没有创建 // 控制器的view懒加载:第一次使用的时候才会去加载,并不是创建UIViewController控制器的时候去加载 UIViewController *vc = [[ViewController alloc] init]; vc.view.backgroundColor = [UIColor redColor]; self.window.rootViewController = vc; // 显示窗口 [self.window makeKeyAndVisible]; return YES; } #pragma mark - 控制器view的默认颜色 - (void)vcViewBackground { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor redColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; btn.center = CGPointMake(20, 100); [self.window addSubview:btn]; // 默认控制器的view:几乎透明 UIViewController *vc = [[UIViewController alloc] init]; vc.view.backgroundColor = [UIColor clearColor]; // vc.view.alpha = 0; // vc.view.backgroundColor = [UIColor yellowColor]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; } @end
// // ViewController.m // 09-控制器View懒加载 #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)loadView { // 如果控制器是窗口的根控制器就可以不用设置尺寸 self.view = [[UIView alloc] initWithFrame:CGRectZero]; self.view.backgroundColor = [UIColor yellowColor]; } //- (UIView *)view //{ // if (_view == nil) { // [self loadView]; // [self viewDidLoad]; // } // return _view; //} // 在viewDidLoad中打印控制器的尺寸不准确,通常在viewDidAppear // 控制器的view加载完成的时候调用 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blueColor]; NSLog(@"%s",__func__); } #pragma mark - view完全显示的时候调用 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"%@",NSStringFromCGRect(self.view.bounds)); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
时间: 2024-10-15 03:19:29