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

來源:互聯網
上載者:User

標籤:

自動布局子視圖

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    //建立父視圖對象    UIView * _superView;    //左上方label    UILabel * _label01;    //右上方label    UILabel * _label02;    //右下角label    UILabel * _label03;    //左下角label    UILabel * _label04;    //中間    UIView * _viewCenter;}@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    _superView =[[UIView alloc]init];    _superView.frame = CGRectMake(20, 20, 180, 280);    _superView.backgroundColor=[UIColor blueColor];    //左上方    _label01 =[[UILabel alloc]init];    //位置相對於父親視圖    _label01.frame=CGRectMake(0, 0, 40, 40);    _label01.text[email protected]"1";    _label01.backgroundColor=[UIColor orangeColor];    //右上方    _label02=[[UILabel alloc]init];    _label02.frame=CGRectMake(180-40, 0, 40, 40);    _label02.text[email protected]"2";    _label02.backgroundColor=[UIColor orangeColor];    //右下角    _label03=[[UILabel alloc]init];    _label03.frame=CGRectMake(180-40, 280-40, 40, 40);    _label03.text[email protected]"3";    _label03.backgroundColor=[UIColor orangeColor];    //左下角    _label04=[[UILabel alloc]init];    _label04.frame=CGRectMake(0, 280-40, 40, 40);    _label04.text[email protected]"4";    _label04.backgroundColor=[UIColor orangeColor];    [_superView addSubview:_label01];    [_superView addSubview:_label02];    [_superView addSubview:_label03];    [_superView addSubview:_label04];    //中間    _viewCenter =[[UIView alloc]init];    _viewCenter.frame=CGRectMake(0, 0, _superView.frame.size.width, 40);    _viewCenter.center = CGPointMake(180/2, 280/2);    _viewCenter.backgroundColor =[UIColor grayColor];    [_superView addSubview:_viewCenter];    [self.view addSubview:_superView];    //自動布局屬性設定,通過此變數來調整視圖在父親視圖中的位置和大小    _viewCenter.autoresizingMask =UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth|    UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin;    //視圖距離父視圖的左側可以變化    _label02.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;    _label03.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |    UIViewAutoresizingFlexibleLeftMargin ;    _label04.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;}-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    static BOOL isLarge = NO;    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:1];    if(isLarge){        _superView.frame=CGRectMake(20, 20, 180, 280);        isLarge=NO;    }else{        _superView.frame=CGRectMake(10, 10, 300, 480);        isLarge=YES;    }    [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.