標籤:
- UIView的autoresizingMask屬性 是用來適應父視圖的變化(根據父視圖的變化進而調整自身Frame屬性)。
@property(nonatomic) BOOL autoresizesSubviews; // default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes@property(nonatomic) UIViewAutoresizing autoresizingMask; // simple resize. default is UIViewAutoresizingNone
UIView 的屬性autoresizesSubviews 預設設定是YES,如果設定為YES,當視圖的bounds變化的時候,子視圖將根據自身的autoresizingMask屬性調節Frame屬性。
- autoresizingMask有以下屬性(預設值為UIViewAutoresizingNone):
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1 << 0, UIViewAutoresizingFlexibleWidth = 1 << 1, UIViewAutoresizingFlexibleRightMargin = 1 << 2, UIViewAutoresizingFlexibleTopMargin = 1 << 3, UIViewAutoresizingFlexibleHeight = 1 << 4, UIViewAutoresizingFlexibleBottomMargin = 1 << 5};
1 #import "TestViewController.h" 2 @interface TestViewController() 3 @property (nonatomic,weak) UIView * superView; 4 @end 5 6 @implementation TestViewController 7 -(void)loadView 8 { 9 [super loadView];10 UIView * superView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 200)];11 superView.backgroundColor = [UIColor redColor];12 [self.view addSubview:superView];13 self.view.backgroundColor = [UIColor whiteColor];14 _superView = superView;15 UIView * subView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 40)];16 superView.autoresizesSubviews = YES;17 18 // 1、設定subView隨superView的bounds變化,上、下、左、右邊距和寬高都隨superView的變化而變化19 subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth;20 // 2、設定subView隨superView的bounds變化,上、下、高都隨superView的變化而變化,而左右邊距和寬度不變21 subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;22 // 3、設定subView隨superView的bounds變化,上邊距和高都隨superView的變化而變化,而下、左、右邊距和寬度不變23 subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;24 // 4、單一屬性一般不做此類設定(沒看出到底什麼效果)25 // subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;26 // 5、設定subView隨superView的bounds變化,下邊距和高都隨superView的變化而變化,而上、左、右邊距和寬度不變27 subView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;28 // 6、設定subView隨superView的bounds變化,左邊距和寬都隨superView的變化而變化,而上、下、右邊距和高度不變29 subView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin;30 // 7、設定subView隨superView的bounds變化,右邊距和寬都隨superView的變化而變化,而上、下、左邊距和高度不變31 subView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin;32 // 8、設定subView隨superView的bounds變化,左、右邊距和寬都隨superView的變化而變化,而上、下邊距和高度不變33 subView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;34 35 36 subView.backgroundColor = [UIColor blackColor];37 [superView addSubview:subView];38 39 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400,100,100)];40 [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];41 [btn setTitle:@"點我" forState:UIControlStateNormal];42 [btn setBackgroundColor:[UIColor grayColor]];43 44 [self.view addSubview:btn];45 }46 47 -(void)click:(id)sender48 {49 _superView.frame = CGRectMake(50, 50, 200, 200);50 }51 @end
- 這裡挑2個參數來說明以下吧。
- 第19行代碼的設定subView隨superView的bounds變化,上、下、左、右邊距和寬高都隨superView的變化而變化
superView開始的frame為(20, 20, 100, 200)點擊按鈕設定為(50, 50, 200, 200)
subView的frame為(0, 20, 50, 40)
subView開始距離superView上邊距為20
subView開始距離superView左邊距為0
subView開始距離superView下邊距為140
subView開始距離superView右邊距為50
調整後superView的frame為(50, 50, 200, 200)後,subView跟隨變化。
superView和之前frame相比,高度無變化,那麼subView相對於superView在上下邊距和高度上自然就沒有相應的變化。
而寬度從100變為200,變大了一倍,那麼相應的subView相對於superView在左右邊距和寬度上也要增加一倍。
subView距離superView上邊距依舊為20*1
subView距離superView左邊距為0*2
subView距離superView下邊距為140*1
subView距離superView右邊距為50*2
subView的高度為40*1
subView的寬度為50*2
- 第29行代碼設定subView隨superView的bounds變化,左邊距和寬都隨superView的變化而變化,而上、下、右邊距和高度不變
superView開始的frame為(20, 20, 100, 200)點擊按鈕設定為(50, 50, 200, 200)
subView的frame為(0, 20, 50, 40)
subView開始距離superView上邊距為20
subView開始距離superView左邊距為0
subView開始距離superView下邊距為140
subView開始距離superView右邊距為50
調整後superView的frame為(50, 50, 200, 200)後,subView跟隨變化。
superView和之前frame相比,高度無變化,那麼subView相對於superView在上下邊距和高度上自然就沒有相應的變化。
而寬度從100變為200,變大了一倍,且subView和superView的右邊距要保持不變左邊距和寬度就需要重新計算。
那麼從100變為200的實際變化其實就要忽略原有的右邊距(其實可以這樣去理解,不跟隨變化的屬性在計算過程中直接減掉對應值在計算變化的倍數)那麼就應該是從(100-50)-(200-50)=1/3那麼右邊距和寬度都應該是之前變化的3倍數。
*(100-50)原來寬度減去固定的距右邊距;(200-50)變化後寬度減去固定的距右邊距。
subView距離superView上邊距依舊為20*1
subView距離superView左邊距為0*3
subView距離superView下邊距為140*1
subView距離superView右邊距為50*1
subView的高度為40*1
subView的寬度為50*3
以上為本人對autoresizingMask屬性的理解,如果有錯誤或者不足的地方歡迎指出。(也可加本人QQ627306590交流,望多多指教。)
iOS8 UIView之autoresizingMask 的使用