UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(10,30,300,30)];

view1.backgroundColor=[UIColor redColor];
[self.window addSubview:view1];
[view1 release];
 
UIView*view2=[[UIView alloc]init];
view2.frame=CGRectMake(30,20,50,100);
view2.backgroundColor=[UIColor blueColor];
[self.window addSubview:view2];
[view2 release];
 
UIView*view3=[[UIView alloc]initWithFrame:CGRectMake(20,50,200,200)];
view3.backgroundColor=[UIColor yellowColor];
[self.window addSubview:view3];
//把某一个view放到最下层
[self.window sendSubviewToBack:view2];
//把某一个view放到最上层
[self.window bringSubviewToFront:view2];
//把某一个view加入到指定层
[self.window insertSubview:view2 atIndex:1];
//把某一个view加入到某层的下面
[self.window insertSubview:view2 belowSubview:view1];
//把某一个view加入到某层的上面
[self.window insertSubview:view2 aboveSubview:view1];
//交换两个层的view
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
 
//自动布局模式(停靠模式)
//前面把_backgroundView设置成了成员变量,为了方便,没有写出来
_backgroundView=[[UIView alloc]initWithFrame:CGRectMake(110,300,100,100)];
_backgroundView.background=[UIColor blackColor];
//设置父view允许子view自动布局
_backgroundView.autoresizesSubviews=YES;
[self.window addSubview:_backgroundView];
 
UIView*topView=[[UIView alloc]initWithFrame:CGRectMake(25,25,50,50)];
topView.backgroundColor=[UIColor orangeColor];
//设置子view的自动布局模式
//下面设置会让topView跟着_backgroundView变化而变化,中心点不变
topView.autoresizingMask=UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
[_backgroundView addSubview:topView];
//创建一个按钮,点一下,_backgroundView会变大
UIButton*btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(10,230,300,20);
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];
 
-(void)click
{
_backgroundView.frame=CGRectMake(_backgroundView.frame.origin.x-2,_backgroundView.frame.orgin.y-2,_backgroundView.frame.size.width+4,_backgroundView.frame.height+4);
}

UIView的层次调整,及子view布局模式自动布局模式(停靠模式)的更多相关文章

  1. IOS UIView 03- 自定义 Collection View 布局

    注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...

  2. View事件传递之父View和子View之间的那点事

    Android事件传递流程在网上可以找到很多资料,FrameWork层输入事件和消费事件,可以参考: Touch事件派发过程详解 这篇blog阐述了底层是如何处理屏幕输,并往上传递的.Touch事件传 ...

  3. LinearLayout的gravity属性以及其子元素的layout_gravity何时有效;RelativeLayout如何调整其子元素位置只能用子元素中的属性来控制,用RelativeLayout中的gravity无法控制!!!

    LinearLayout的gravity属性以及其子元素的layout_gravity何时有效:RelativeLayout如何调整其子元素位置只能用子元素中的属性来控制,用RelativeLayou ...

  4. 15-UIKit(view布局、Autoresizing)

    目录: 1. 纯代码布局 2. 在View中进行代码布局 3. Autoresizing 回到顶部 1. 纯代码布局 纯代码布局分VC下和V下 [MX1-layout-code] 在VC下覆盖view ...

  5. viewpager与子view的事件冲突解决

    问题: 对android的事件机制一直不怎么了解,最近android项目中运用viewpager+listview (就是viewpager的子view中嵌套了listview),出现了触摸手势冲突 ...

  6. Android View框架总结(六)View布局流程之Draw过程

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52236145 View的Draw时序图 ViewRootImpl.p ...

  7. Android View框架总结(五)View布局流程之Layout

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52216195 View树的Layout流程 View的Layout时序图 View布局 ...

  8. Android View框架总结(四)View布局流程之Measure

    View树的measure流程 View的measures时序图 View布局流程之measure measure过程 View的measure过程 ViewGroup的measure过程 Frame ...

  9. Listview自定义了子View导致listview的onitemclick事件无效

    原因是子View的点击事件抢占了listview的点击事件 解决办法: 1. 子View根布局 设置 android:descendantFocusability="blocksDescen ...

随机推荐

  1. Filter责任链模式

    Filter责任链的创建 org.apache.catalina.core.ApplicationFilterFactory#createFilterChain,  此方法是被org.apache.c ...

  2. 在新项目下使用rbc权限

    例子说明: 8 个步骤 1. 拷贝rabc这个app到项目中 2. 把迁移过来的rbac下migrations记录删除 3. 把rabc注册到项目中 4. 创建数据库迁移 5. 注册admin,录入数 ...

  3. java——sleep()和wait()

    1.所属类不同sleep()属于Thread而wait()属于Object 2.sleep()没有释放当前锁,wait()则释放了当前锁 3.sleep(0)的目的是使当前线程释放cpu,其他线程可以 ...

  4. QQ号_保存_20180124

    1.1337978907 (密码:33+我的名字全小写全部拼音) 2. 3. 4. 5.

  5. 转 python3中SQLLIT编码与解码之Unicode与bytes

    #########sample########## sqlite3.OperationalError: Could not decode to UTF-8 column 'logtype' with ...

  6. Jmeter录制pc脚本

    1.打开jmeter后可以看到左边窗口有个“测试计划”和“工作台”,右键“测试计划”,添加 Threads(Users) →线程组,再右键 线程组→添加 配置元件→Http请求默认值 Http请求默认 ...

  7. SQL事务的四种隔离级别

    1未提交读(Read uncommitted):完全不锁表,所以会出现脏数据.2提交读(Read committed):1.事务1中update才锁表,可以select到最新数据. 事务2select ...

  8. 386. Lexicographical Numbers 把1--n按字典序排序

    https://leetcode.com/problems/lexicographical-numbers/description/ 前20个是 1, 10, 11, 12, 13, 14, .... ...

  9. Unity Input.GetMouseButtonDown 拿到鼠标按键

    //点击按键,生成子弹,并射向前方 void ShootBullet() { if (Input.GetMouseButtonDown(0)) { GameObject temp_Buller = G ...

  10. nodejs操作文件

    var fs = require('fs'); var txt = "以上程序使用fs.readFileSync从源路径读取文件内容,并使用fs.writeFileSync将文件内容写入目标 ...