天天看點

螢幕适配、Autoresizing、Autolayout螢幕适配、Autoresizing、Autolayout、

螢幕适配、Autoresizing、Autolayout、

标簽(空格分隔): ios基礎

螢幕适配

iPhone4之前

  • 沒有适配,不用适配
  • 經常會出現坐标值寫死的代碼
//#define ScreenW 320
//#define ScreenH 480
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

// 建立一個顯示在螢幕右下角的按鈕
CGFloat buttonW = ;
CGFloat buttonH = ;
button.frame = CGRectMake(ScreenW - buttonW, ScreenH - buttonH, buttonW, buttonH);
           

iPad出現以後

  • 需要橫豎屏适配
  • 出現了一種友善的螢幕适配技術:Autoresizing
    • 局限性:僅僅能解決子控件和父控件之間的相對關系問題

iOS6開始(Xcode4開始)

  • 出現了一種新的螢幕适配技術:Autolayout
    • 解決任何控件之間的相對關系問題

iOS8開始(Xcode6開始)

  • 出現了一種新的螢幕适配技術:Sizeclass

Autoresizing

注意點:

- UIViewAutoresizingFlexible

Left

Margin = 1 << 0,

- 距離父控件

左邊

的間距是伸縮的

- UIViewAutoresizingFlexible

Right

Margin = 1 << 2,

- 距離父控件

右邊

的間距是伸縮的

- UIViewAutoresizingFlexible

Top

Margin = 1 << 3,

- 距離父控件

上邊

的間距是伸縮的

- UIViewAutoresizingFlexible

Bottom

Margin = 1 << 5

- 距離父控件

下邊

的間距是伸縮的

- UIViewAutoresizingFlexible

Width

= 1 << 1,

-

寬度

跟随父控件

寬度

進行伸縮

- UIViewAutoresizingFlexible

Height

= 1 << 4,

-

高度

跟随父控件

高度

進行伸縮

Autolayout

2個核心概念

  • 限制
    • 尺寸限制
      • width限制
      • height限制
    • 位置限制
      • 間距限制(上下左右間距)
  • 參照
    • 所添加的限制跟哪個控件有關(相對于哪個控件來說)

常見單詞

  • Leading -> Left -> 左邊
  • Trailing -> Right -> 右邊

UILabel實作包裹内容

  • 設定寬度限制為 <= 固定值
  • 設定位置限制
  • 不用去設定高度限制