autoresizing 使用

(一) autoresizing 簡(jiǎn)介

autoresize的使用

  •  autoresizing  和 autolayout  是 互斥的
    
  • autoresizing 外四根, 內(nèi) 兩根
    
    • 外四根: 控制子view 到父view邊界的距離是否變化
      
    • 內(nèi)兩根: 子view的size 是否會(huì)跟隨父viewsize的變化而變化
      
    • 位枚舉, 可以同時(shí)設(shè)置 多個(gè)值

    view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    3.autoresizing 的弊端 , 不能設(shè)置 任意子控件之間的參照(約束)


(二) 代碼示例

-(void)viewDidLoad {
[super viewDidLoad];

// 實(shí)例化兩個(gè)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];


// 對(duì) blueView 進(jìn)行設(shè)置
/**
 位枚舉, 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
 */

/**
 左側(cè)保持不變  - 右側(cè)靈活
 右側(cè)保持不變  - 左側(cè)靈活
 底部保持不變
 寬度保持變化
 */
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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容