前言:現在早已不是「只有一種尺寸」的時代,光 iPhone 開發至少就得適配三種尺寸。過去我們可以用絕對座標硬寫控制項位置,現在這招行不通了。你可能會說那做兩套、三套 UI 就好——但那既不高效,也不符合設計原則。
iOS 有兩大自動布局利器:autoresizing 與 Auto Layout(後者是 iOS 6 之後新增)。autoresizing 是 UIView 的屬性,一直存在,使用簡單,但能力不如 Auto Layout 強大。如果你的介面比較簡單、細節要求不高,那麼光用 autoresizing 做自動布局完全夠用。以下就針對 autoresizing 來討論。
0、使用前的基本說明
UIViewAutoresizing 是一個列舉型別,預設值為 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
};各值說明:
UIViewAutoresizingNone
不隨父視圖的改變而改變UIViewAutoresizingFlexibleLeftMargin
自動調整 view 與父視圖的左邊距,以保證右邊距不變UIViewAutoresizingFlexibleWidth
自動調整 view 的寬度,保證左右邊距都不變UIViewAutoresizingFlexibleRightMargin
自動調整 view 與父視圖的右邊距,以保證左邊距不變UIViewAutoresizingFlexibleTopMargin
自動調整 view 與父視圖的上邊距,以保證下邊距不變UIViewAutoresizingFlexibleHeight
自動調整 view 的高度,以保證上下邊距都不變UIViewAutoresizingFlexibleBottomMargin
自動調整 view 與父視圖的下邊距,以保證上邊距不變這裡要特別提醒:如果你平常習慣在 Storyboard/Xib 裡設定 autoresizing,改用程式碼設定時很容易理解錯。例如 UIViewAutoresizingFlexibleTopMargin,很容易被誤解成「頂部距離不變」,實際上是「底部距離不變」。解法也很簡單——記住程式碼寫法與 Storyboard 的設定是相反的即可。
autoresizing 的組合使用:
列舉值可以用 | 串接,同時具備多個值的特性,以因應不同場景。例如:
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin
意思是:view 的寬度隨父視圖寬度等比縮放,距父視圖頂部的距離保持不變。其他組合同理,這裡就不一一列舉了。
注意:
1)只有當 view 的 autoresizesSubviews 屬性為 yes(預設就是 yes)時,autoresizing 才會生效。
2)從 Xcode 6 開始,Storyboard 與 Xib 預設啟用自動布局,因此我們得手動切換才能使用 autoresizing。
具体操作如下圖(打開 Storyboard 檔案就會看到這個畫面):

下面寫一個簡單範例幫助理解,文末會附上 Demo 下載連結。
Demo 目標:
1)頂部距父視圖的距離不變
2)寬度隨父視圖等比拉伸
3)view 與父視圖的左右邊距不變

一、用程式碼控制 autoresizingMask
下面是專案中用到的巨集:
#define topSpace 64 #define kMargin 20 #define kTopViewHeight 44 #define kTopViewWidth 300 #define kTextLabelWidth 200 #define kTextLabelHeight 30
還沒做適配之前的程式碼:
// 以 Iphone4(320, 480)為基礎,設置各控件的位置 // 注意:必須所有控件都按照 Iphone4(320, 480)為基礎初始化一次,不然按比例縮放時會發生錯誤! UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(kMargin, topSpace, kTopViewWidth, kTopViewHeight)]; CGFloat textLabelTop = (topView.frame.size.width - kTextLabelWidth) / 2; CGFloat textLabelWidth = (topView.frame.size.height - kTextLabelHeight) / 2; UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textLabelTop, textLabelWidth, kTextLabelWidth, kTextLabelHeight)]; // 設置文字及居中 [textLabel setText:@"Garvey"]; [textLabel setTextAlignment:NSTextAlignmentCenter]; // 分別設置樣式 [topView setBackgroundColor:[UIColor redColor]]; [textLabel setTextColor:[UIColor whiteColor]]; // 添加視圖 [topView addSubview:textLabel]; [self.view addSubview:topView];
執行結果:

改用 autoresizing 做適配:
補充:你可以先以其他裝置的尺寸初始化介面上的控制項,因為 autoresizing 會隨父視圖的變化而調整。
// 以 Iphone4(320, 480)為基礎,設置各控件的位置 // 注意:必須所有控件都按照 Iphone4(320, 480)為基礎初始化一次,不然按比例縮放時會發生錯誤! UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(kMargin, kTopSpace, kTopViewWidth, kTopViewHeight)]; CGFloat textLabelTop = (topView.frame.size.width - kTextLabelWidth) / 2; CGFloat textLabelWidth = (topView.frame.size.height - kTextLabelHeight) / 2; UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textLabelTop, textLabelWidth, kTextLabelWidth, kTextLabelHeight)]; // 設置文字及居中 [textLabel setText:@"Garvey"]; [textLabel setTextAlignment:NSTextAlignmentCenter]; // 分別設置樣式 [topView setBackgroundColor:[UIColor redColor]]; [textLabel setTextColor:[UIColor whiteColor]]; // 設置文字控件的寬度按照上一級視圖(topView)的比例進行縮放 [textLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; // 設置 View 控件的寬度按父視圖比例縮放,距父視圖頂部、左邊距和右邊距的距離不變 [topView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin]; // 添加視圖 [topView addSubview:textLabel]; [self.view addSubview:topView]; // 注意:重新設置 topView 位置的程式碼,必須寫在添加視圖的後面,不然 autoresizing 的位置計算會出錯! CGFloat topViewWidth = kUIScreen.size.width - kMargin * 2; [topView setFrame:CGRectMake(kMargin, kTopSpace, topViewWidth, kTopViewHeight)];
最後顯示:

二、在 Storyboard 中控制 autoresizingMask
Storyboard 上只有兩個控制項:View 與 Label。

如果完全不設適配,執行結果是:

預設會貼齊左上方,在 Autoresizing 面板上可以看到:

要達到目標效果其實很簡單,兩個控制項如下設定即可:

意思是:
1)頂部距父視圖的距離不變
2)寬度隨父視圖等比拉伸
3)view 與父視圖的左右邊距不變
最後顯示:

小結:
對比這兩種做法,用程式碼控制 autoresizing 顯然麻煩一些;用 Storyboard/Xib 就簡單多了,點幾下就好。看你想選哪種。
Demo 下載:https://pan.baidu.com/s/1qWnxsZU