手動布局子視圖
先看今天要做的效果圖:

這裡預設用 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]; //建立一個父視圖 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]; // 釋放可重建的資源 } @end
這裡的關鍵在於:四個小視圖的位置是拿 self.bounds 算的,所以一旦父視圖尺寸改變並觸發 layoutSubviews,子視圖就會自動重新貼齊四角——不用在外層手動重算座標。把「布局邏輯寫進 layoutSubviews」是手動布局裡非常值得養成的習慣。希望對大家的學習有幫助。