iOS开发之手动布局子视图

所属分类: 软件编程 / IOS 阅读数: 1577
收藏 0 赞 0 分享

手动布局子视图;
下面先看下效果图,我们今天要实现的效果:


这里我们默认用storyboard启动:
首先我们要在白色的屏幕上面创建一个父视图SuperView(蓝色的背景),在父视图里面创建四个小视图(橘黄色的背景)
下面看代码,
在SuperView.h文件里面:

#import <UIKit/UIKit.h>

@interface SuperView : UIView{

 UIView * _view01;
 UIView * _view02;
 UIView * _view03;
 UIView * _view04;

}

//声明创建视图函数
-(void) createSubViews;

@end

在SuperView.m文件里面:


#import "SuperView.h"

@interface SuperView ()

@end

@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)layoutSubviews{

 [UIView beginAnimations:nil 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];

}


@end

在ViewController.m文件里面:

#import "ViewController.h"
#import "SuperView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.

 //创建一个父视图
 SuperView * sView = [[SuperView alloc]init];
 sView.frame = CGRectMake(20, 20, 180, 280);

 //父视图调用函数创建四个小视图
 [sView createSubViews];

 sView.backgroundColor = [UIColor blueColor];
 [self.view addSubview:sView];

 UIButton * btn01 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 btn01.frame = CGRectMake(240, 480, 80, 40);
 [btn01 setTitle:@"放大" forState:UIControlStateNormal];
 [btn01 addTarget:self action:@selector(pressLarge) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:btn01];

 UIButton * btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 btn02.frame = CGRectMake(240, 520, 80, 40);
 [btn02 setTitle:@"缩小" forState:UIControlStateNormal];
 [btn02 addTarget:self action:@selector(pressSmall) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:btn02];

 sView.tag = 101;


}

//放大父视图
-(void) pressLarge{
 SuperView * sView = (SuperView*)[self.view viewWithTag:101];
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1];
 sView.frame=CGRectMake(20, 20, 280, 400);
 [UIView commitAnimations];

}

//缩小父视图

-(void) pressSmall{
 SuperView * sView = (SuperView*)[self.view viewWithTag:101];
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1];
 sView.frame=CGRectMake(20, 20, 180, 280);
 [UIView commitAnimations];
}


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

@end

 以上代码书写完毕,就达到了上面视图的效果,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

iOS App中UILabel的自定义及在Auto Layout中的使用

这篇文章主要介绍了iOS App中UILabel的自定义及在Auto Layout中的使用,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS应用开发中UITableView的分割线的一些设置技巧

这篇文章主要介绍了iOS应用开发中UITableView分割线的一些设置技巧,包括消除分割线的方法,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

详解iOS时间选择框

这篇文章主要为大家详细介绍了详解iOS时间选择框,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解iOS App开发中UIViewController的loadView方法使用

这篇文章主要介绍了详解iOS App开发中UIViewController的loadView方法使用,讲解了访问view属性时loadView方法的调用及使用loadView时的一些注意点,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在iOS App中自定义和隐藏状态栏的方法

这篇文章主要介绍了在iOS App中自定义和隐藏状态栏的方法,在顶部时某些状况下即用应用内的状态栏覆盖系统本身的,代码示例为Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发实现音频播放功能

本文给大家分享的是在IOS开发过程中实现音频播放的功能,讲解的十分细致,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

IOS开发实现录音功能

本文给大家分享的是一个IOS开发中实现录音功能的实例,并简单给大家解析一下,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS实现图片轮播效果

这篇文章主要为大家详细介绍了IOS实现图片轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS通过http post上传图片

这篇文章主要介绍了iOS通过http post上传图片的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS框架Spring常用的动画效果

本文给大家介绍的是在IOS开发中常用的动画效果以及自定义转场动画特效的代码,非常的简单实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多