一、创建控制器的几种方法
<span style="font-size:14px;">self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//需要一个根控制器
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];</span>
1.加载箭头
UIViewController *vc = [mainStoryboard instantiateInitialViewController];
2.通过标识
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"blueVC"];
3.xib
DetailViewController *vc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
4.代码
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
二、视图操作
UIView *orangeView = [[UIView alloc] init];
//CGRectGetMidx 获取当前frame中心点的x
orangeView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
orangeView.bounds = CGRectMake(0, 0, 200, 200);
orangeView.backgroundColor = [UIColor orangeColor];
//设置圆角
orangView.layer.cornerRadius = 10;
//设置边框
orangView.layer.borderWidth = 2;
//设置边框颜色
orangView.layer.borderColor = [UIColor yellowColor].CGColor;
//设置透明度
orangView.alpha = 1.0;
//隐藏
// orangView.hidden = YES;
//将多余的部分剪切掉(超出父视图的部分剪切了)
// orangView.clipsToBounds = YES;
//设置阴影
orangView.layer.shadowColor = [UIColor yellowColor].CGColor;
//设置阴影偏移量
orangView.layer.shadowOffset = CGSizeMake(10, 10);
//设置阴影透明度
orangView.layer.shadowOpacity = 0.5;
orangView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:orangView];
UIView *blueView = [[UIView alloc]init];
blueView.frame = CGRectMake(80, 50, 200, 200);
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
//交换上下层视图(根据索引交换视图)
// [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
UIView *redView = [[UIView alloc]init];
redView.frame = CGRectMake(50, 10, 200, 200);
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
//根据索引交换位置
//[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:0];
//把某个视图放到最前面
// [self.view bringSubviewToFront:blueView];
//把某个视图放到最后面
// [self.view sendSubviewToBack:redView];
//插入
UIView *purView = [[UIView alloc]init];
purView.frame = CGRectMake(100, 60, 200, 200);
purView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:purView];
// 将某个视图插入到指定视图的上方
[self.view insertSubview:purView aboveSubview:blueView];
// 将某个视图插入到指定视图的下方
[self.view insertSubview:purView belowSubview:redView];
//移除子视图
//获取所有子视图
// NSArray *array = self.view.subviews;
//移除子视图
// [purView removeFromSuperview];
//移除所有的子视图:遍历移除
// for (UIView *view in array) {
// [view removeFromSuperview];
// }
}
- (void)buttonClick{
//模态切换(推送控制器)
nextViewController *nextvc = [[nextViewController alloc]init];
[self presentViewController:nextvc animated:YES completion:nil];
}