UI02视图和视图控制器

一、创建控制器的几种方法

<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];
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值