本篇博客,教学目标:

UIView是UIKit中部分控件的直接或者间接的父类。

UIView的层次和事件接收
【1】父视图不能接收事件,则子视图无法接收事件
【2】子视图超出父视图部分,不能接收事件
【3】同一个父视图下,最上面的视图,首先遭遇事件,如果能够响应,就不向下传递事件,如果不能响应,事件向下传递.

1.在视图A上加载视图B,那么视图B就是视图A的子视图,视图A就是视图B的父视图。父视图可以有很多子视图,但是子视图只能有一个父视图

    CGFloat left = 20;CGFloat top = 30;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];

2.后添加的子视图,显示在最上面。多个子视图的frame如果有重合部分,我们不能通过视觉效果判断谁是谁的父视图,必须通过superview方法来判断父视图

    CGFloat left = 20;CGFloat top = 100;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];view1.tag = 101;[self.view addSubview:view1];UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view2.backgroundColor = [UIColor blueColor];view2.tag = 102;[self.view addSubview:view2];UIView * superView1 = view1.superview;UIView * superView2 = view2.superview;if (superView1 == superView2) {NSLog(@"view1和view2是同一个父视图");}

3.在某个子视图上面插入一个子视图

    CGFloat left = 20;CGFloat top = 170;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];top = top+20;UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view2.backgroundColor = [UIColor yellowColor];[self.view insertSubview:view2 aboveSubview:view1];

4.在某个子视图下面插入一个子视图

    CGFloat left = 20;CGFloat top = 260;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];top = top+20;UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view2.backgroundColor = [UIColor greenColor];[self.view insertSubview:view2 belowSubview:view1];

5.交换两个子视图在父视图上的位置

    CGFloat left = 20;CGFloat top = 340;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];top = top+20;UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view2.backgroundColor = [UIColor orangeColor];[self.view addSubview:view2];NSArray *subviews = [self.view subviews];NSInteger index1 = [subviews indexOfObject:view1];NSInteger index2 = [subviews indexOfObject:view2];[self.view exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];

6.将某个子视图提到所有子视图的最前面

    CGFloat left = 20;CGFloat top = 340;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];top = top+20;UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view2.backgroundColor = [UIColor grayColor];[self.view addSubview:view2];top = top+20;UIView * view3 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view3.backgroundColor = [UIColor brownColor];[self.view addSubview:view3];[self.view bringSubviewToFront:view2];

7.将某个子视图扔到所有子视图的最后面

    CGFloat left = 20;CGFloat top = 460;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];top = top+20;UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view2.backgroundColor = [UIColor grayColor];[self.view addSubview:view2];top = top+20;UIView * view3 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];view3.backgroundColor = [UIColor brownColor];[self.view addSubview:view3];[self.view sendSubviewToBack:view2];

8.从父视图上移除某个子视图

    CGFloat left = 20;CGFloat top = 560;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.backgroundColor = [UIColor redColor];[self.view addSubview:view1];[view1 removeFromSuperview];

9.设置视图透明度

CGFloat left = 20;CGFloat top = 615;CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;CGFloat height = 50;UIView * view1 = [[UIView alloc] init];view1.frame = CGRectMake(left, top, width, height);view1.alpha = 0.5;view1.backgroundColor = [UIColor blackColor];[self.view addSubview:view1];

github地址:demo

自学iOS开发系列----UI(视图编程入门:UIView)相关推荐

  1. iOS开发系列--网络开发(转)

    iOS开发系列--网络开发 2014-10-22 08:34 by KenshinCui, 66365 阅读, 56 评论, 收藏, 编辑 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微 ...

  2. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook详解

    代码改变世界 Posts - 69, Articles - 0, Comments - 812 Cnblogs Dashboard Login Home Contact Gallery RSS Ken ...

  3. iOS开发系列--网络开发

    概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...

  4. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如 ...

  5. iOS开发系列–音频播放、录音、视频播放、拍照、视频录制

    iOS开发系列–音频播放.录音.视频播放.拍照.视频录制 - KenshinCui - 博客园 代码改变世界 Posts - 69, Articles - 0, Comments - 1004 Cnb ...

  6. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总,icloudpassbook

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总,icloudpassbook --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS ...

  7. iOS开发系列--通知与消息机制

    http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣 ...

  8. iOS开发系列--UITableView全面解析

    iOS开发系列--UITableView全面解析 2014-08-23 23:20 by KenshinCui, 2202 阅读, 18 评论, 收藏,  编辑 --UIKit之UITableView ...

  9. iOS开发系列文章(持续更新……)

    iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...

  10. iOS开发系列–打造自己的“美图秀秀”(上)

    iOS开发系列–打造自己的"美图秀秀"(上) 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框架.今天我们将围绕iOS中两 ...

最新文章

  1. Android 动画之TranslateAnimation应用详解
  2. 【Kotlin】Lambda 表达式 ( 简介 | 表达式语法 | 表达式类型 | 表达式返回值 | 调用方式 | 完整示例 )
  3. lucene.net 应用资料
  4. Dynamics CRM 2013 初体验(1):系统的安装
  5. 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波3 -幂律变换、伽马变换
  6. 第10步 (1)logback.xml日志配置(2) ftp(上传文件)服务器配置(3) idea注入和自动编译配置(4)项目提交gitee(5)fe助手和restlet client
  7. 重磅 | 《中国移动云网一体产品白皮书(2021)》发布!
  8. 应届生去软通动力怎么样_超强干货:应届生如何找实习工作?
  9. NodeJS 数组超出部分以弹出框显示。
  10. python运维脚本部署jdk_Python+PyDev+Jdk+Eclipse开发环境部署
  11. css背景图片不显示原因,excel 背景图片(css设置背景图片不显示问题)
  12. 48种世间哲学,其中值得借鉴的有10个
  13. 次世代建模之人体比例及肌肉骨骼介绍
  14. STM32单片机OLED俄罗斯方块单片机小游戏
  15. 创建个人网页,创建个人网址。
  16. ASAM_CCP_V2.1.0_cn
  17. angularjs grunt uglify 报错
  18. 微信小程序组件库——colorUI 的使用方法
  19. 游戏图片文件和声音文件的隐藏
  20. 最长匹配括号子序列问题

热门文章

  1. 订单管理_03查询订单信息流程
  2. G - A Question of Ingestion 动态规划
  3. 小象学院python网课值得吗-小象学院的机器学习集训营课程怎么样?
  4. 基于stm32f103c8t6的fft频率计
  5. ong拼音汉字_汉语拼音中ong怎么读
  6. 8.随机生成一个矩阵并对其转置
  7. 无线服务器功能,无线自组织互联网的用户管理——Radius服务器的功能设计与实现...
  8. p2p与反p2p的激战--资料搜集
  9. 程序员须学计算机语言,新手程序员需要学什么编程语言
  10. 如何快速提高自己的领导力?