码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI-自动布局(Autoresizing)

时间:2016-01-16 14:14:00      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

 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 @end
16 
17 @implementation ViewController
18 
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,高为50
57      右边有一个redView它的右边和底部与父控件间距也是固定为20
58      两个View等宽,等高,
59      redView离blueView之间的间距永远是20.演示不能做出这个效果,然后引入Auto Layout
60      
61      */
62 }
63 
64 - (void)change
65 {
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)

标签:

原文地址:http://www.cnblogs.com/oc-bowen/p/5135324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!