学习IOS之处,不知道如何创建UI,现记录几种方式方便后续查看
1.不使用ViewController直接创建,如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blueColor];
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
view.backgroundColor = [UIColor redColor];
[self.window addSubview:view];
[view release];
[self.window makeKeyAndVisible];
return YES;
}
2.使用ViewController,自定义loadView,如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blueColor];
ViewController *rc=[[ViewController alloc] init];
self.window.rootViewController=rc;
[self.window makeKeyAndVisible];
return YES;
}
// ViewController
- (void)loadView {
UIView* view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.backgroundColor=[UIColor yellowColor];
self.view=view;
}
// 如果ViewController没有重写loadView,则self.window则会创建一个empty view
3.使用xib
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blueColor];
TestViewController* rc = [[TestViewController alloc] initWithNibName:@"View" bundle:nil];
self.window.rootViewController = rc;
[self.window makeKeyAndVisible];
return YES;
}
4.使用storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 获取某个storyboard
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// 使用这个此storyboard的初始viewcontroller
//self.window.rootViewController = [storyBoard instantiateInitialViewController];
// 使用这个此storyboard的identifier为hello的viewtorller
self.window.rootViewController = [storyBoard instantiateViewControllerWithIdentifier:@"hello"];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;