zoukankan      html  css  js  c++  java
  • UIView的层次调整,及子view布局模式自动布局模式(停靠模式)

    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);
    }
  • 相关阅读:
    9th week
    8th Week 2
    8th Week 1
    课后作业-阅读任务-阅读提问-4
    2017-11-30-构建之法:现代软件工程-阅读笔记
    《团队-OldNote-项目总结》
    个人编程作业2-课程总结
    《团队-Oldnote-最终程序》
    课后作业-阅读任务-阅读提问-3
    《20171102-构建之法:现代软件工程-阅读笔记》
  • 原文地址:https://www.cnblogs.com/youlechang123/p/5724464.html
Copyright © 2011-2022 走看看