iOS 第二课 创建视图

0:删除3个文件ViewController.hViewController.mMain.storyboard


1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空


2:视图的创建,

首先还是以window 为底,其次添加到window上面的视图是一层一层的叠加上去的,我们可以通过这个按钮打开视图,来查看每一层

例如:



//
//  AppDelegate.m
//  SecondNEWUIDEMO
//
//  Created by 千雅爸爸 on 16/10/8.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //所有的其它的视图都必须要添加window上面
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    [self.window setRootViewController:[[UIViewController alloc ] init]];
    
    //视图的创建,相对于父视图
    UIView *view  = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
    [view setBackgroundColor:[UIColor orangeColor]];
    
    
    //将view 添加到window
    [self.window addSubview:view];
    
    //再来创建一个,添加顺序决定层次,这个和java的frmelayout相类似
    //视图层次我们可以通过下面的视图中的导航的肩头旁边的两个方框叠起来的按钮
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 400)];
    [view1 setBackgroundColor:[UIColor cyanColor]];
    [self.window addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
    [view2 setBackgroundColor:[UIColor blueColor]];
    [self.window addSubview:view2];
    
    //
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 200)];
    [view3 setBackgroundColor:[UIColor yellowColor]];
    //设置透明度
    [view3 setAlpha:0.6f];//0为全透明,1为全不透明的状态
    [self.window addSubview:view3];
    
    //所有的视图都可以添加子视图,这个和android 有很大的区别,android只能是viewgroup 去添加子诗图,ios是view就可以
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 200)];
    [view4 setBackgroundColor:[UIColor greenColor]];
    [view1 addSubview:view4];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

视图控制器的自定义:

首先即成UIViewController去创建一个MyUIViewController

//
//  MyUIViewController.h
//  SecondNEWUIDEMO
//
//  Created by 千雅爸爸 on 16/10/8.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyUIViewController : UIViewController

@end

//
//  MyUIViewController.m
//  SecondNEWUIDEMO
//
//  Created by 千雅爸爸 on 16/10/8.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "MyUIViewController.h"

@interface MyUIViewController ()

@end

@implementation MyUIViewController

//如何使用视图控制器来控制我们的视图
//那么我们就需要自定义一个UIViewController MyUIViewController
//只能有一个根视图管理器,其实这个视图管理器也是有颜色的
//在自定义的MyUIViewController里面去重写viewDidLoad
//有了自定义的视图管理器以后,可以将代码不写在我们这个appdelegate里面了,可以直接卸载自定义的视图管理器里面了

//如何判断是否已经被加载了
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"已经被加载了");
    //其实这个视图管理器也是有颜色的
    [self.view setBackgroundColor:[UIColor redColor]];
    
    //可以不用在appdelegate 里面添加了,在这里添加就可以了
    UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(50,50, 500, 500)];
    [view6 setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:view6];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

//
//  AppDelegate.m
//  SecondNEWUIDEMO
//
//  Created by 千雅爸爸 on 16/10/8.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "AppDelegate.h"
#import "MyUIViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //所有的其它的视图都必须要添加window上面
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    //[self.window setRootViewController:[[UIViewController alloc ] init]];
    //如何使用视图控制器来控制我们的视图
    //那么我们就需要自定义一个UIViewController MyUIViewController
    //只能有一个根视图管理器,其实这个视图管理器也是有颜色的
    //在自定义的MyUIViewController里面去重写viewDidLoad
    //有了自定义的视图管理器以后,可以将代码不写在我们这个appdelegate里面了,可以直接卸载自定义的视图管理器里面了
    [self.window setRootViewController:[[MyUIViewController alloc ] init]];
    
    //视图的创建,相对于父视图
    UIView *view  = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
    [view setBackgroundColor:[UIColor orangeColor]];
    
    
    //将view 添加到window
    [self.window addSubview:view];
    
    //再来创建一个,添加顺序决定层次,这个和java的frmelayout相类似
    //视图层次我们可以通过下面的视图中的导航的肩头旁边的两个方框叠起来的按钮
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 400)];
    [view1 setBackgroundColor:[UIColor cyanColor]];
    [self.window addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
    [view2 setBackgroundColor:[UIColor blueColor]];
    [self.window addSubview:view2];
    
    //
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 200)];
    [view3 setBackgroundColor:[UIColor yellowColor]];
    //设置透明度
    [view3 setAlpha:0.6f];//0为全透明,1为全不透明的状态
    [self.window addSubview:view3];
    
    //所有的视图都可以添加子视图,这个和android 有很大的区别,android只能是viewgroup 去添加子诗图,ios是view就可以
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 200)];
    [view4 setBackgroundColor:[UIColor greenColor]];
    [view1 addSubview:view4];
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值