1.创建一个UIWindow
//初始化 [[UIScreen mainScreen] bounds 是一个Frame CGRect 也可以自定义 用CGRectMake
//self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];意思就是一个大小200*200 原点在0,0的框
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//定义背景颜色 可以用rgb颜色定义 r g b取值为0~1
//self.window.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
//或者使用图片
//UIImage *backImage = [UIImage imageNamed:@"background"];
//self.window.backgroundColor = [UIColor colorWithPatternImage:backImage];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
2.创建一个UIView
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
myView.backgroundColor = [UIColor redColor];
//把myView加载到UIWindow上
[self.window addSubview:myView];
//设置myView的高度
[myView setFrame:CGRectMake(0,20,100,100)];
3.创建一个UIViewController
//MyFirstViewController 由自己定义在项目中
MyFirstViewController *viewController = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
//viewController的view会被自动加载 具体看api帮助
self.window.rootViewController = viewController;
4.在UIViewController添加简单控件
在自定义的UIViewController添加UIButton UILabel 其余的类似 当然也可以加一个新的UIView
//添加UIButton
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 200, 50)];
button.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.6];
[button setTitle:@"test" forState:UIControlStateNormal];
[self.view addSubview:button];
//为button添加一个事件
[button addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//添加UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 200, 50)];
label.text=@"test";
[self.view addSubview:label];
5.控制UIViewController跳转
模态(modal):a弹出b a为presenting view controller b为presented view controller
-(void)someButtonClicked
{
[self presentViewController:self.aVC animated:YES completion:^{
}];
}
消失 直接写在b中 已做优化[self dismissViewControllerAnimated:YES completion:^{
}];
如设置委托 在被弹出vc中设协议 弹出vc中实现 可实现传值 和任意层跳回 中间的层将直接消失 最后那个会有动画弹出式的动画风格 设置presented VC的modalPresentationStyle属性
UIModalPresentationFullScreen;//默认的;
UIModalPresentationPageSheet;
UIModalPresentationFormSheet;
UIModalPresentationCurrentContext;