iOS開發從入門到精通--手動布局子視圖

來源:互聯網
上載者:User

標籤:

手動布局子視圖;
下面先看下,我們今天要實現的效果:

這裡我們預設用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開發從入門到精通--手動布局子視圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.