手動結構子視圖;
上面先看下後果圖,我們明天要完成的後果:

這裡我們默許用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開辟之手動結構子視圖】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!