iOS aotoresizing的基本使用

来源:互联网 发布:什么是绿色版软件 编辑:程序博客网 时间:2024/04/24 10:07

//

//  ViewController.m

//  自动布局01

//

//  Created by 唐帅 on 16/4/16.

//  Copyright © 2016 tang. All rights reserved.

//


#import "ViewController.h"


@interfaceViewController ()

@property(nonatomic,weak)UIView *blueView;

@property(nonatomic,weak)UIView *redView;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    UIView *blueView = [[UIViewalloc]init];

    blueView.backgroundColor = [UIColorblueColor];

    blueView.frame =CGRectMake(100,100, 100,100);

    self.blueView = blueView;

    [self.viewaddSubview:blueView];

    

    

    UIView *redView = [[UIViewalloc]init];

    redView.backgroundColor = [UIColorredColor];

    redView.frame =CGRectMake(25,25, 50,50);

    self.redView = redView;

    [blueView addSubview:redView];

    

    

//    给红色view添加约束

//    UIViewAutoresizingNone                 = 0,

//    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,


//    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,

//    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,


//    UIViewAutoresizingFlexibleBottomMargin = 1 << 5

    

    //让红色的view向右下跟随蓝色view移动

    self.redView.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;

}


//触摸屏幕发生的事件

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    CGRect bounds =self.blueView.bounds;

    bounds.size.width +=50;

    bounds.size.height +=50;

    

    self.blueView.bounds = bounds;

    

    NSLog(@"%@",NSStringFromCGRect(self.blueView.bounds));

}

@end

0 0
原创粉丝点击