iOS开发之Autoresizing记录
2018-06-26 本文已影响66人
朱晓晓的技术博客
Autoresizing,仅限于父子控件之间的操作,对于兄弟控制器的操作是无效的
拖控件
1.取消自动布局
image.png1.设置布局
image.png纯代码
- (void)viewDidLoad {
[super viewDidLoad];
//autoresizing
UIView *bgView = [[UIView alloc]init];
bgView.backgroundColor = [UIColor orangeColor];
bgView.frame = CGRectMake(0, 0, 200, 250);
[self.view addSubview:bgView];
self.bgView = bgView;
UIView *childView = [[UIView alloc]init];
childView.frame = CGRectMake(150, 150, 50, 100);
childView.backgroundColor = [UIColor redColor];
[bgView addSubview:childView];
childView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleLeftMargin;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGFloat w = 100 +arc4random_uniform(100);
CGFloat h = 100+ arc4random_uniform(100);
self.bgView.frame = CGRectMake(0, 0, w, h);
}
重点提示 UIViewAutoresizing
UIViewAutoresizingNone = 0, 什么都没有
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,距离右边固定
UIViewAutoresizingFlexibleRightMargin = 1 << 2,距离左边固定
UIViewAutoresizingFlexibleTopMargin = 1 << 3,距离底部固定
UIViewAutoresizingFlexibleBottomMargin = 1 << 5 距离顶部固定
以上的几个属性跟咱们拖控件时候是相反的
UIViewAutoresizingFlexibleWidth = 1 << 1,宽度随父控件自行伸缩
UIViewAutoresizingFlexibleHeight = 1 << 4,宽度随父控件自行伸缩