autoresizing 使用

2016-07-14  本文已影响22人  dicesc

(一) autoresizing 简介

autoresize的使用


(二) 代码示例

-(void)viewDidLoad {
[super viewDidLoad];

// 实例化两个view
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

self.redView = redView;

redView.backgroundColor = [UIColor redColor];

[self.view addSubview:redView];


UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];

blueView.backgroundColor = [UIColor blueColor];

[redView addSubview:blueView];


// 对 blueView 进行设置
/**
 位枚举, option 枚举
 
 Flexible : 灵活的 , 可变化的
 
 UIViewAutoresizingNone                 = 0,        0
 0000 0000 
 
 UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,   1
 0000 0001  --> 1 * 2 0次方
 
 UIViewAutoresizingFlexibleWidth        = 1 << 1,   2
 0000 0010
 
 UIViewAutoresizingFlexibleRightMargin  = 1 << 2,   4
 0000 0100
 
 UIViewAutoresizingFlexibleTopMargin    = 1 << 3,   8
 0000 1000
 
 UIViewAutoresizingFlexibleHeight       = 1 << 4,   16
 0001 0000
 
 UIViewAutoresizingFlexibleBottomMargin = 1 << 5    32
 0010 0000
 */

/**
 左侧保持不变  - 右侧灵活
 右侧保持不变  - 左侧灵活
 底部保持不变
 宽度保持变化
 */
blueView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

}

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

CGRect tempBounds = _redView.bounds;

tempBounds.size.width += 20;
tempBounds.size.height += 20;

_redView.bounds = tempBounds;

}

上一篇 下一篇

猜你喜欢

热点阅读