IOS创建UI的方式

学习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;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值