iOS開發(fā)之Autoresizing記錄

Autoresizing,僅限于父子控件之間的操作,對(duì)于兄弟控制器的操作是無效的

拖控件

1.取消自動(dòng)布局
image.png
1.設(shè)置布局
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);
}

重點(diǎn)提示 UIViewAutoresizing

     UIViewAutoresizingNone                 = 0, 什么都沒有
     UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,距離右邊固定
     UIViewAutoresizingFlexibleRightMargin  = 1 << 2,距離左邊固定
     UIViewAutoresizingFlexibleTopMargin    = 1 << 3,距離底部固定
     UIViewAutoresizingFlexibleBottomMargin = 1 << 5 距離頂部固定

     以上的幾個(gè)屬性跟咱們拖控件時(shí)候是相反的

     
     UIViewAutoresizingFlexibleWidth        = 1 << 1,寬度隨父控件自行伸縮
     UIViewAutoresizingFlexibleHeight       = 1 << 4,寬度隨父控件自行伸縮

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容