• iOS UI-自动布局(Autoresizing)


     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
  • 相关阅读:
    通信—互联网协议入门2(转载)
    通信—互联网协议入门1(转载)
    通信—HTTP 协议入门(转载)
    通信—跨域资源共享
    ionic3使用echart插件
    通信—HTTP与HTTPS
    ionic学习笔记—常用命令
    ionic学习笔记—创建项目
    Spark 资源池简介
    数据库设计原则
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5135324.html
Copyright © 2020-2023  润新知