UIVIew的常用方法,属性

代码实例(包括层级关系,这两段都只是ViewController.m)

//
//  ViewController.m
//  UIView
//
//  Created by 姜凯文 on 2018/7/20.
//  Copyright © 2018年 姜凯文. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //创建一个UIView对象
    //UIView时iOS视图对象
    //显示在屏幕上的所有对象的基础类
    //所有显示在屏幕上的对象一定都继承于UIView
    //屏幕上能看到的对象都是UIView的子类
    //UIView是一个矩形对象,有背景颜色,可以显示,有层级关系
    UIView* view = [[UIView alloc] init];

    //设置UIView的位置
    view.frame = CGRectMake(100, 100, 100, 200);

    view.backgroundColor = [UIColor greenColor];

    //将我们新建的试图添加到父亲视图上
    //1.将我们新建的视图显示到屏幕上
    //2.将视图作为父亲视图的子视图管理起来
    [self.view addSubview:view];

    //是否隐藏视图对象
    //YES:不显示
    //NO:显示, 默认NO
    view.hidden = NO;

    //设置视图的透明度
    //alpha = 1:不透明
    //a = 0: 透明
    //alpha = 0.5 半透明
    view.alpha = 0.5;

    self.view.backgroundColor = [UIColor blueColor];

    //设置时都显示不透明
    view.opaque = YES;

    //将自己从父亲视图删除
    //1.从父亲视图的管理删除掉
    //2.不会显示在屏幕上
    [view removeFromSuperview];
}


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


@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建三个视图对象
    UIView* view01 = [[UIView alloc] init];

    view01.frame = CGRectMake(100, 100, 150, 150);

    view01.backgroundColor = [UIColor blueColor];

    UIView* view02 = [[UIView alloc] init];

    view02.frame = CGRectMake(125, 125, 150, 150);

    view02.backgroundColor = [UIColor orangeColor];

    UIView* view03 = [[UIView alloc] init];

    view03.frame = CGRectMake(150, 150, 150, 150);

    view03.backgroundColor = [UIColor greenColor];

    //将三个对象显示到屏幕
    //并且添加到父亲视图上
    //哪一个视图被先添加就先绘制
    [self.view addSubview:view01];

    [self.view addSubview:view02];

    [self.view addSubview:view03];

    //将某一个视图调整到最前面显示
    //参数:UIView对象,调整那一个视图到最前面
    [self.view bringSubviewToFront:view03];

    //将某一个视图调整到最后面显示
    //参数:UIView对象,调整那一个视图到最后面
    [self.view sendSubviewToBack:view02];

    //subviews管理所有self.view的子视图的数组
    //就是说这里的顺序是按照看上去的顺序,第一个就是最后面的,最后一个就是最上面的
    UIView* viewFront = self.view.subviews[2];

    UIView* viewBack = self.view.subviews[0];

    if (viewBack == view02) {
        NSLog(@"相等");
    }

    [view01 removeFromSuperview];
    //真正删除直接不会相等
}


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


@end

心得体会

  1. 讲了三种从屏幕上让UIView消失的方法
    • 透明度
    • 是否隐藏
    • 移去
    • 注意的是第三种是真正移去,也就是连触发事件都不能,而前两种都只是看不见
  2. 层级关系就两句话
    • 先出现的在下面
    • subviews数组的顺序是重叠顺序,最下面的第一个
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值