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

IOS开发 手动布局子视图

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

本节学习内容:

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];

}

相关文章

  • IOS开发 手动布局子视图

    本节学习内容: 1.子视图布局的概念 2.视图布局的创建实现 3.手动布局视图的实现 1.创建一个父视图 命名为S...

  • 子视图的布局(手动、自动)

    解决的主要问题:使子视图的位置相对于父视图固定 手动布局子视图 创建视图 layoutSubviews 关键方法:...

  • IOS开发 自动布局子视图

    本节学习内容: 1.自动子视图布局的概念 2.自动布局视图的创建 3.自动布局子视图的实现 【ViewContro...

  • 手动和自动布局子视图

    1. 创建一个父视图和多个子视图 新建一个类,叫做SuperView,作为父亲视图类。 在SuperView.h中...

  • OC之view手动布局视图

    1、设置子视图的frame 布局子视图:使用该方法直接设置子视图的frame 在iOS 5.1和更早版本上这个方法...

  • iOS开发-视图渲染与性能优化

    iOS开发-视图渲染与性能优化 iOS开发-视图渲染与性能优化

  • layoutSubviews、setNeedsLayout 和

    layoutSubviews 方法 布局子视图。 此方法的默认实现在 iOS 5.1 及更早的版本中什么都不会做。...

  • 【转载】iOS 模态视图

    iOS 模态视图 概念: ios开发中,在当前视图上再弹出一个视图(模态视图)例如登陆视图,分享视图,注册等等。 ...

  • 性能优化

    iOS 保持界面流畅的技巧 iOS开发-视图渲染与性能优化

  • iOS-自定义控件相关

    本篇涵盖自定义控件、视图等. 1.iOS开发-轻松学会封装自定义视图view(自定义弹框封装详解)2.iOS开发-...

网友评论

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

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