美文网首页
IOS开发 自动布局子视图

IOS开发 自动布局子视图

作者: 奔跑的小小鱼 | 来源:发表于2017-05-18 22:22 被阅读52次

本节学习内容:

1.自动子视图布局的概念

2.自动布局视图的创建

3.自动布局子视图的实现


【ViewController.h】

#import<UIKit/UIKit.h>

@interface Viewcoontroller:UIViewController{

//创建父亲视图对象

UIView*_superView;

//左上角label

UILable* _label01;

//右上角

UILable* _label02;

//右下角

UILable* _label03;

//左下角

UILable* _label04;

UIView* _viewCenter;

}

@end


【ViewController.m】

#import"ViewController.h"

@interface ViewController()

@end

@implementation ViewConroller

-(void)viewdidLoad{

[super viewDidLoad];

//创建父视图

_superView=[[UIView alloc] init];

_superView.frame=CGRectMake[20,20,180,280);

_superView.backgroundColor=[UIColor blueColor];

//创建lable

_label01=[[UILabel alloc]init];

//位置相对于父亲视图

_label01.frame=CGRectMake(0,0,40,40);

_label01=[[UILabel alloc]init];

_label01.text=@"1";

_label01.backgroundColor=[UIColor greenColor];

_label02=[[UILabel alloc]init];

_label02.frame=CGRectMake(180-40,40,40,40);

_label02.text=@"2";

_label02.backgroundColor=[UIColor greenColor];

_label03=[[UILabel alloc]init];

_label03.frame=CGRectMake(180-40,280-40,40,40);

_label03.text=@"3";

_label03.backgroundColor=[UIColor greenColor];

_label04=[[UILabel alloc]init];

_label04.frame=CGRectMake(0,280-40,40,40);

_label04.text=@"4";

_label04.backgroundColor=[UIColor greenColor];

_superView addSubview:_label01];

_superView addSubview:_label02];

_superView addSubview:_label03];

_superView addSubview:_label04];

[self.view addSubview:_superView];

_viewCenter=[[UIView allc]init];

_viewCenter.frame=CGRectMake(0,0,_superView.bounds.size.widith,40);

_viewCenter.center=CGPointMake(180/2,280/2);

_viewCenter.backgroundColor={UIColor orangeColor];

[_superView addSubview:_viewCenter];

//自动布局属性设置,通地此变理来调整视图在父视图中的位置和大小FlexibleHeight;指高度,Flexiblewidth:指宽度

//距离上,下,宽度都是可变的

_viewCenter.autoresizingMask=UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleTopMargin UIViewAutoresizingFlexibleBottomMargin;

//视图距离父亲视图的左侧可变化

_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:nill];

[UIView setAnimationDuration:1];

if(isLarge==NO){

_superView.frame=CGRectMake(10,10,300,480);

}else{

_superView.frame=CGRectMake(20,20,180,280);

[UIView commitAnimatios];

isLarge=!isLarge

}

相关文章

网友评论

      本文标题:IOS开发 自动布局子视图

      本文链接:https://www.haomeiwen.com/subject/yuiqxxtx.html