標籤:
1 // 2 // ViewController.m 3 // IOS_0115_buzhi 4 // 5 // Created by ma c on 16/1/15. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h"10 11 @interface ViewController ()12 @property (nonatomic, strong) UIView *myView;13 14 15 @end16 17 @implementation ViewController18 19 - (void)viewDidLoad {20 [super viewDidLoad];21 22 UIView *blue = [[UIView alloc] init];23 blue.frame = CGRectMake(0, 0, 200, 200);24 blue.backgroundColor = [UIColor blueColor];25 [self.view addSubview:blue];26 self.myView = blue;27 28 UIView *red = [[UIView alloc] init];29 red.backgroundColor = [UIColor redColor];30 red.frame = CGRectMake(0, blue.frame.size.height-50, blue.frame.size.width, 50);31 [blue addSubview:red];32 33 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];34 [btn setFrame:CGRectMake(self.view.center.x - 50, self.view.center.y + 150, 50, 50)];35 [btn setBackgroundColor:[UIColor purpleColor]];36 37 [self.view addSubview:btn];38 39 [btn addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];40 41 //設定autoresizing(前提取消autolayout)42 //設定顯示規則,只能按照父控制項來設定參照43 red.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;44 45 /*46 位枚舉47 UIViewAutoresizingNone = 0,48 UIViewAutoresizingFlexibleLeftMargin = 1 << 0,距離父控制項的右邊是固定的49 UIViewAutoresizingFlexibleWidth = 1 << 1,寬度隨著父控制項變化而變化50 UIViewAutoresizingFlexibleRightMargin = 1 << 2,距離左邊是固定的51 UIViewAutoresizingFlexibleTopMargin = 1 << 3,距離下邊是固定的52 UIViewAutoresizingFlexibleHeight = 1 << 4,高度隨著父控制項變化而變化53 UIViewAutoresizingFlexibleBottomMargin = 1 << 5距離上面是固定的54 55 Autoresizing的弊端56 在storyboard中示範一個blueView左邊和底部與父控制項間距固定20,高為5057 右邊有一個redView它的右邊和底部與父控制項間距也是固定為2058 兩個View等寬,等高,59 redView離blueView之間的間距永遠是20.示範不能做出這個效果,然後引入Auto Layout60 61 */62 }63 64 - (void)change65 {66 CGRect frame = self.myView.frame;67 frame.size.width +=20;68 frame.size.height +=20;69 self.myView.frame = frame;70 71 }72 73 - (void)didReceiveMemoryWarning {74 [super didReceiveMemoryWarning];75 // Dispose of any resources that can be recreated.76 }77 78 @end
iOS UI-自動布局(Autoresizing)