close

 

Swift - AutoLayout

目的:安排調整版面VIEW的尺寸位置不寫code

View無論在直式、橫式的畫面,其padding及margin長寬都一樣,不會改變。

autoLayout

autoLayout_結果2

autoLayout_結果3               autoLayout_結果4

 


1. New ProjectSingleView Application 

autoLayout

2. Product Name:AutoLayoutSample

    Lanuage:Swift

autoLayout2   

 3.拉三個View,尺寸各為:

左上&右上:W*H=130*254

下方:W*H=250*254

目標上下左右中間的padding&margin均為20

先以iPhone小尺寸版面作設定: 取消選取Use Auto Layout及Use Size Classes

autoLayout3

 4.左上View Autoresizing :

autoLayout4

 5.右上View Autoresizing :

autoLayout5

 6.下方的View Autoresizing :

autoLayout6

  7.iPhone小尺寸的結果: 直式没問題

autoLayout7

8.iPhone小尺寸的結果: 橫式view之間padding變小

autoLayout8

9.再調整:選取最外圍的View,Autoresizing

autoLayout9

 10.改變版面:選取wAny hAny:移動藍色框架,版面就會隨之變動

autoLayout10

11.結果:和預期的結果,還是有落差

autoLayout11          

  autoLayout13         autoLayout12autoLayout14

  • 再次調整每個View之間的padding及margin:均為20
  • 利用Editor/Pin的Widths及Heights做調整
  • 若有橘色線表示仍然有限制constrats需要調整,一直到標示線全變為藍色為止。

autoLayout27

  12.Shift加選上方二個View->Editor/Pin/Widths Equally

      Shift加選上方三個View->Editor/Pin/Heights Equally

autoLayout15      autoLayout16

 13.調整margin及padding:

 

  • 選取左上方View->Editor/Pin/Top Space to Superview
  • 選取左上方View->Editor/Pin/Leading Space to Superview
  • 選取右上方View->Editor/Pin/Top Space to Superview
  • 選取右上方View->Editor/Pin/Trailing Space to Superview

 

autoLayout18 

 14.調整margin及padding:

 

  • 選取下方View->Editor/Pin/Leading Space to Superview
  • 選取下方View->Editor/Pin/Trailing Space to Superview
  • 選取下方View->Editor/Pin/Bottom Space to Superview

 

autoLayout19

  15.調整margin:

選取下方View的Trailing Space:constraint改為20

autoLayout17

  16.調整margin及paddiing:有橘色線表示仍然有限制constrats需要調整,一直到標示線全變為藍色為止。

autoLayout20  

 17.調整padding:

選取左上方及下方View->Editor/Pin/Vertical Spacing

AutoLayout 

 18.調整margin及padding:

點選黃色箭頭, 出現警告訊息,double click update Frame

autoLayout22

autoLayout23autoLayout25

 19.結果:

 autoLayout  

 autoLayout28      autoLayout29 autoLayout30 autoLayout31  


ps.about  wAny hAny

autoLayout2autoLayout3  

 

 

ref: http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1

arrow
arrow
    文章標籤
    swift autolayout
    全站熱搜

    Patty 發表在 痞客邦 留言(0) 人氣()