1、打开Xcode创建一个工程,为了纯代码实现,删除多余文件。
2、创建一个MainViewController类继承UIViewController
3、在AppDelegate.m中编写代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建一个Window视图,设置为与设备尺寸大小
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置Window的颜色
[self.window setBackgroundColor:[UIColor greenColor]];
//将视图展示出来
[self.window makeKeyAndVisible];
//设置window为根视图。
[self.window setRootViewController:[[MainViewController alloc] init]];
return YES;
}
4、在MainViewController.m中编写代码:
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Controller loader");
//创建一个view视图,坐标(20.20)大小(200.200)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 320, 320)];
//设置view视图的颜色
[view setBackgroundColor:[UIColor orangeColor]];
//将view添加到父视图
[self.view addSubview:view];
//再添加一个视图
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 300, 300)];
[view1 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:view1];
//继续添加一个视图
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 260, 260)];
[view2 setBackgroundColor:[UIColor purpleColor]];
//设置透明度为0.5。注意透明度的使用对他的子视图有影响
[view2 setAlpha:0.8];
[self.view addSubview:view2];
//在view2上添加一个子视图
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 120, 120)];
[view3 setBackgroundColor:[UIColor yellowColor]];
[view2 addSubview:view3];}
- (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
效果预览图