你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發從入門到精通--自動布局子視圖

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

編輯:IOS開發綜合

這裡寫圖片描述

#import 

@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=@"1";
    _label01.backgroundColor=[UIColor orangeColor];

    //右上角
    _label02=[[UILabel alloc]init];
    _label02.frame=CGRectMake(180-40, 0, 40, 40);
    _label02.text=@"2";
    _label02.backgroundColor=[UIColor orangeColor];

    //右下角
    _label03=[[UILabel alloc]init];
    _label03.frame=CGRectMake(180-40, 280-40, 40, 40);
    _label03.text=@"3";
    _label03.backgroundColor=[UIColor orangeColor];

    //左下角
    _label04=[[UILabel alloc]init];
    _label04.frame=CGRectMake(0, 280-40, 40, 40);
    _label04.text=@"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 *)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
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved