Autoresizing,僅限于父子控件之間的操作,對于兄弟控制器的操作是無效的
拖控件
1.取消自動布局
image.png
1.設置布局
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,寬度隨父控件自行伸縮