iOS UI-自動布局(Autoresizing)

來源:互聯網
上載者:User

標籤:

 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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.