UIView 子view跟随父view动态变化

news/2024/7/20 23:04:35 标签: iOS

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect frame = [[UIScreen mainScreen] bounds];
    
    UIView *view1 = [[UIView alloc]init];
    view1.frame = CGRectMake(100, 100, 100, 100);
    view1.backgroundColor = [UIColor blackColor];
    //允许子视图自适应
    view1.tag = 100;
    view1.autoresizesSubviews = YES;
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(25, 25, 50, 50);
    view2.backgroundColor = [UIColor whiteColor];
    //设置子视图适应方式
    view2.autoresizingMask =
    UIViewAutoresizingFlexibleLeftMargin   |
    UIViewAutoresizingFlexibleWidth        |
    UIViewAutoresizingFlexibleRightMargin  |
    UIViewAutoresizingFlexibleTopMargin    |
    UIViewAutoresizingFlexibleHeight       |
    UIViewAutoresizingFlexibleBottomMargin ;
    [view1 addSubview:view2];
    
    UIButton *btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(0, frame.size.height-200, 50, 50);
    btn.backgroundColor = [UIColor grayColor];
    [btn setTitle:@"变化" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void)click:(UIButton *)btn{
   UIView *view1 =  [self.view viewWithTag:100];
    CGRect frame = view1.frame;
    view1.frame = CGRectMake(frame.origin.x-20, frame.origin.y-20, frame.size.width+40, frame.size.height+40);
}



UIView的视图自适应

父级view1中,包含一个view2,view1的大小变化时,view2可以根据设定条件,与view1一同变化:

view1先设置属性 ,使子view可以动态改变大小:

view1.autoresizesSubviews =YES;


view2子控件设置autoresizeingMask属性,来决定父view变化,子view做出怎样相应的变化:

    view2.autoresizingMask =

    UIViewAutoresizingFlexibleLeftMargin   |

    UIViewAutoresizingFlexibleWidth        |

    UIViewAutoresizingFlexibleRightMargin  |

    UIViewAutoresizingFlexibleTopMargin    |

    UIViewAutoresizingFlexibleHeight       |

    UIViewAutoresizingFlexibleBottomMargin ;







http://www.niftyadmin.cn/n/1398947.html

相关文章

UILabel 的简单实用

UILabel *label1 [[UILabel alloc]init];label1.frame CGRectMake(20, 20, 200, 200);label1.backgroundColor [UIColor yellowColor];//文本label1.text "hello,hello,hello,hello,hello,hello,hello,hello,hello,";//文字颜色label1.textColor [UIColor black…

关于ios自动布局

原理:IOS6.0 之后,苹果优化了UI界面的布局方式,提出了自动布局的概念,和之前的autoresizing相比功能更强大。子视图基于父视图的自动布局显示。都是父视图去添加对子视图的约束。 在这里主要说的是通过代码对自动布局视图的实现。…

ios 往 Library的cache目录下 存放缓存数据的方法

// 写缓存(void)writeLocalCacheData:(NSData *)data withKey:(NSString *)key {// 设置存储路径NSString *cachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:key];// 判读缓存数据是否存在if (…

RecycleView + Fragment 替换 ViewPager + Fragment

ViewPager Fragment 用了好多年了,想换个用法,研究了一下 RecycleView Fragment 其中 RecycyleView用到了 PagerSnapHelper 实现了滑动页面效果有什么好的建议,希望大家提出! Github https://github.com/nerveva2000/RecycleVi…

ERP、CRM、SCM、电子商务、BI、ITSS

ERP 企业资源规划 Enterprise Resource Planning 思想 1.事前计划2.事中控制3.事后分析4.及时调整 四个发展阶段 1.基本MRP --无生产能力控制 物资需求计划(Material Requirement Planning)聚焦于相关物资需求的问题,根据主生产计划、物料清单…

信息系统_服务_审计_资质_监理

信息系统 目的 以处理信息为目的专门的系统类型 信息系统可以是手动的,也可以是计算机化的 计算机化的信息系统的组成部分 (硬软库网储感外人规) 1.硬件 由执行输入、处理、输出行为的计算机设备组成 2.软件 有管理计算机运行的程序构成&…

J2EE和.NET技术

J2EE和.NET技术 J2EE 将开发工作分类 业务逻辑开发 和 表示逻辑开发 J2EE服务器运行环境包括 1.组件(Component) 表示应用逻辑的代码 2.容器(Container) 是组件的运行环境 3.服务(Services) 是应用服务提供…

信息、信息技术、信息系统、软件、信息化、企业信息化

信息 定义 1. 香农(信息论的奠基人) 是有别于 物质和能量 的 第三种东西,是对事物运动状态和存在方式的不确定性的描述(无法预测不可知的)(事物运动状态和状态变化的自我描述) 2. 维纳 信息就是信息,既不是物质,也不是…