码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发从入门到精通--自动布局子视图

时间:2016-08-03 17:03:37      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

自动布局子视图
技术分享

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    //创建父视图对象
    UIView * _superView;

    //左上角label
    UILabel * _label01;
    //右上角label
    UILabel * _label02;
    //右下角label
    UILabel * _label03;
    //左下角label
    UILabel * _label04;
    //中间
    UIView * _viewCenter;
}


@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.

    _superView =[[UIView alloc]init];
    _superView.frame = CGRectMake(20, 20, 180, 280);
    _superView.backgroundColor=[UIColor blueColor];

    //左上角
    _label01 =[[UILabel alloc]init];
    //位置相对于父亲视图
    _label01.frame=CGRectMake(0, 0, 40, 40);
    _label01.text=@"1";
    _label01.backgroundColor=[UIColor orangeColor];

    //右上角
    _label02=[[UILabel alloc]init];
    _label02.frame=CGRectMake(180-40, 0, 40, 40);
    _label02.text=@"2";
    _label02.backgroundColor=[UIColor orangeColor];

    //右下角
    _label03=[[UILabel alloc]init];
    _label03.frame=CGRectMake(180-40, 280-40, 40, 40);
    _label03.text=@"3";
    _label03.backgroundColor=[UIColor orangeColor];

    //左下角
    _label04=[[UILabel alloc]init];
    _label04.frame=CGRectMake(0, 280-40, 40, 40);
    _label04.text=@"4";
    _label04.backgroundColor=[UIColor orangeColor];

    [_superView addSubview:_label01];
    [_superView addSubview:_label02];
    [_superView addSubview:_label03];
    [_superView addSubview:_label04];

    //中间
    _viewCenter =[[UIView alloc]init];
    _viewCenter.frame=CGRectMake(0, 0, _superView.frame.size.width, 40);
    _viewCenter.center = CGPointMake(180/2, 280/2);
    _viewCenter.backgroundColor =[UIColor grayColor];
    [_superView addSubview:_viewCenter];

    [self.view addSubview:_superView];

    //自动布局属性设置,通过此变量来调整视图在父亲视图中的位置和大小
    _viewCenter.autoresizingMask =UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth|
    UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin;

    //视图距离父视图的左侧可以变化
    _label02.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

    _label03.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleLeftMargin ;

    _label04.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

}


-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    static BOOL isLarge = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];

    if(isLarge){
        _superView.frame=CGRectMake(20, 20, 180, 280);
        isLarge=NO;
    }else{
        _superView.frame=CGRectMake(10, 10, 300, 480);
        isLarge=YES;
    }

    [UIView commitAnimations];

}

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

@end

iOS开发从入门到精通--自动布局子视图

标签:

原文地址:http://blog.csdn.net/android_it/article/details/52104965

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!