IOS开发 手动布局子视图

本节学习内容:

1.子视图布局的概念

2.视图布局的创建实现

3.手动布局视图的实现


1.创建一个父视图 命名为SuperView生成,Superview.h与SuperView.m两个文件

【Superview.h】

import<UIKit/UIKit.h>

@interface SuperView:UIView{

UIView* _view01;

UIView* _view02;

UIView* _view05;

UIView* _view04;

UIView* _view05;

}

-(void) createsubViews;

@end

【Superview.m】

#import"SuperView.h"

@implementation SuperView

-(void)createSubViews{

//左上角视图

_view01=[[UIView alloc]init];

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

//右上角视图

_view02=[[UIView alloc]init];

_view02.frame=CGRectMake(self.bounds.size.width-40,0,40,40);

//右下角视图

_view03=[[UIView alloc]init];

_view03.frame=CGRectMake(self.bounds.size.width-40,self.bounds.size.height-40,40,40);

//左下角视图

_view04=[[UIView alloc]init];

_view04.frame=CGRectMake(0,self.bounds.size.height-40,40,40);

_view01.backgroundColor=[UIColor orangeColor];

_view02.backgroundColor=[UIColor orangeColor];

_view03.backgroundColor=[UIColor orangeColor];

_view04.backgroundColor=[UIColor orangeColor];

[self addsubview:_view01];

[self addsubview:_view02];

[self addsubview:_view03];

[self addsubview:_view04];

}

//需要重新布局时调用此函数,通过此函数重新设定子视图的位置,手动调整子视图位置

-(void)layoutSubvies{

[UIView beginanimations:nill context:nil];

[UIView setAnimationDuration:1];

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

_view02.frame=CGRectMake(self.bounds.size.width-40,0,40,40);

_view03.frame=CGRectMake(self.bounds.size.width-40,self.bounds.size.height-40,40,40);

_view04.frame=CGRectMake(0,self.bounds.size.height-40,40,40);

[UIView commitAnimations];

}


【ViewController.m】

#import"ViewController.h";

#import"SuperView.h";

@interface ViewController()

@end

@implementation ViewController

-(void)viewDidLoad{

[super ViewdidLoad];

//创建一个父亲视图

SuperView* sView=[[SuperView allock]init];

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

[sView createSubViews];

sView.backgroundColor=[UIColor blueColor];

[self.view addSubview:sView];

UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

btn.frame=CGRectMake(240,480,80,40);

[btn setTitle:@"放大" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(pressLarge) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

UIButton* btn02=[UIButton buttonWithType:UIButtonTypeRoundedRect];

btn02.frame=CGRectMake(240,520,0,40);

[btn02 setTitle:@"缩小" forState:UIControlStateNormal];

[btn02 addTarget:sef action:@selector(pressSmall)forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn02];

sView.tag=101;

}

//放大父亲视图

-(void)pressLarge{

//放大父亲视图动画

SuperView* SView=(SuperView*)[self.view viewWithTag:101];

[UIView beginanimations:nill context:nil];

[UIView setAnimationDuration:1];

sView.frame=CGRectMake(20,20,300,480);

[UIView commitAnimations];

}

//缩小父亲视图

-(void)pressSmall{

SuperView* SView=(SuperView*)[self.view viewWithTag:101];

[UIView beginanimations:nill context:nil];

[UIView setAnimationDuration:1];

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

[UIView commitAnimations];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容