ã¹ãã¼ãªã¼ãã¼ãã§Autolayout
Xcode 5.1.1
æã ã¯ããAutolayoutããéãããã¨ã¯ã§ããªãã
ä»ããã§ãAutolayoutã使ããã¨ã«æ £ãã¦ãããããªãã以ä¸ã«Autolayoutã使ã£ãå ·ä½çãªãã¶ã¤ã³ä¾ã¨æé ãæ²è¼ãã¦ããã®ã§ãAutolayoutã«ã¾ã ä¸æ £ãã ã¨ããã²ã¨ã«ããã¥ã¼ããªã¢ã«ã¨ãã¦åç §ãã¦ããã ãããã
iPhoneã§ãããªç»é¢ãä½ã£ã¦ã¿ãã¨ããã
å³ä¸ã«ããããå·¦ä¸ã«ããã¿ãä¸å¤®ã«ã¡ã¤ã³ã³ã³ãã³ããé ç½®ãã¦ããã
ãã®ç»é¢ãAutolayoutã使ããã«ã絶対座æ¨ã§ä½ã£ãã¨ããã¨ãããã¤ã¹ã横åãã«ããã¨ãã«ã¬ã¤ã¢ã¦ããå´©ãã¦ãã¾ãã
ã4ã¤ã³ãç»é¢ - 横åãã
ã¢ããªã®ãã«ãè¨å®ã§æ¨ªåãã«å¯¾å¿ããªãã¨ããæãããããä»ã®æ代ã¯ãããããã¤ã¹ã«ãã£ã¦ç»é¢ã®ã¢ã¹ãã¯ãæ¯ãç°ãªãããåç¥ã®ããã«ãiPhone5, 5c, 5sï¼4ã¤ã³ãï¼åãã«ã¬ã¤ã¢ã¦ãããç»é¢ãiPhone 4s以åï¼3.5ã¤ã³ãï¼ããã¤ã¹ã§è¡¨ç¤ºããã¨ãç»é¢ä¸é¨ãåãæ¨ã¦ããã¦ãã¾ãã
iPhone 5ãåºãå½åãããã¯ã¾ã ã3.5ã¤ã³ãåãã«ç»é¢ããã¶ã¤ã³ãã¦ã4ã¤ã³ãã§ã¯ç»é¢ä¸é¨ãåãæ¨ã¦ããã¦ãæ§ããªãããã«ã¬ã¤ã¢ã¦ããããã¨ããã£ããããããªãããããããããæ代ã§ã¯ãªããªã£ã¦ããã
ä»æ¹ããªãã®ã§Autolayoutã使ã£ã¦ã¿ããã
ä»åã®ã¬ã¤ã¢ã¦ãè¦ä»¶ã以ä¸ã®ããã«å®ç¾©ãã¦ã¿ãã
ãä»åã®ãé¡ã
- ãããé¨ã®å¹ ã¨é«ãã¯åºå®ã§ããã¨ããã
- ãããé¨ã®å¹ ã¨ããã¿é¨ã®å¹ ã¯åããµã¤ãºã§ããã¨ããã
- ãããé¨ã®é«ãã¨ããã¿é¨ã®é«ãã¯åããµã¤ãºã§ããã¨ããã
- ãããé¨ã¯ç»é¢ã®ä¸ãã8pxãå³ãã8pxã®ä½ç½®ã«é ç½®ãããã¨ã¨ããã
- ããã¿é¨ãç»é¢ã®ä¸ãã8pxãå·¦ãã8pxã®ä½ç½®ã«é ç½®ãããã¨ã¨ããã
- ã¡ã¤ã³ã³ã³ãã³ãé¨ã¯å·¦å³8pxã®ãã¼ã¸ã³ãåããä¸é¨ã¯ãããé¨ã®ä¸ãã8pxãä¸é¨ã¯ããã¿é¨ã®ä¸ãã8pxã®ä½ç½®ã«é ç½®ããã
ãã¦ãåãã¦Autolayoutã使ãã¨ããåæãããã¦ä½ãããã°è¯ãã®ãããããã«ã硬ç´ãã¦ãã¾ãã®ã¯ç§ã ãã§ã¯ãªãã ãããStoryboardä¸ã§Autolayoutãå®ç¾©ããåæã¨ãã¦ã¯ã次ã®2éããããã
ã2éãã®åæã
ã»ç»é¢ä¸ã®ãAlignããã¿ã³ãæ¼ãã
ã»ç»é¢ä¸ã®ãPinããã¿ã³ãæ¼ãã
ä¸è¨SSã«ããããããAlignãã¿ã³ï¼èµ¤ï¼ã¨Pinãã¿ã³ï¼ç´ºï¼ã®ä½ç½®ã«ç®å°ãã¤ãã¦ãããã
ãAlignã¨Pinã§ã§ãããã¨ã
- 1ã¤ã®ãªãã¸ã§ã¯ããé¸æãã¦ããPinããé¸æãããªãã¸ã§ã¯ãã®åºå®å¹ ãåºå®é«ããå®ç¾©ããã
- 1ã¤ã®ãªãã¸ã§ã¯ããé¸æãã¦ããPinããé¸æãããªãã¸ã§ã¯ãã®è¦ªãã¥ã¼ã®ä¸ä¸å·¦å³ã«å¯¾ããããããã®ééãå®ç¾©ããã
- 2ã¤ã®ãªãã¸ã§ã¯ããé¸æãã¦ããPinããé¸æãã2ã¤ã®ãªãã¸ã§ã¯ãã®å¹ ãé«ããåãå¤ã«è¨å®ããã
- 2ã¤ã®ãªãã¸ã§ã¯ããé¸æãã¦ããAlignããé¸æãã2ã¤ã®ãªãã¸ã§ã¯ãéã®ä½ç½®é¢ä¿ãå®ç¾©ããã
ç´°ããããã°ãã£ã¨ãããããããªãããã¾ãã¯ãã®ãããã®ãã¨ãã§ããã¨è¦ãã¦ããã°ããã ããã
ãã¦ãããã§ã¯ãã£ããããããé¨ããã¬ã¤ã¢ã¦ããã¦ããã
ã»ãããé¨ã®ãã¥ã¼ãé¸æããã
ã»Pinã¢ã¤ã³ã³ãã¯ãªãã¯ããã
ã»å¶ç´ãè¨å®ãããã¤ã¢ãã°ãåºãã®ã§ã以ä¸ã®ããã«ããã
親ãã¥ã¼ã®ä¸ãã8pxãå³ãã8pxã®å¹
ãã»ãããï¼ä¸ã¨å³ã¸ã®ç¹ç·ã赤ãå®ç·ã«å¤ãã£ããã¨ã«æ³¨ç®ï¼
å¹
ã160pxãé«ãã44pxã«ã»ãããã¦ããAdd 4 Constraintsããæ¼ãã
ã¹ãã¼ãªã¼ãã¼ãã®æ§é ããªã¼ãå±éããã¨ã4ã¤ã®å¶ç´ãåºæ¥ã¦ãããã¨ããããã
ããã§å³ä¸ã«ãé»è²ãå³ç¢å°ã表示ããã¦ãããã¨æãããã®é»è²ãç¢å°ã¯è¦åã§ãããããã¤ãã¯ãªãã¯ããã¨ããã¨ãã°ä»¥ä¸ã®ãããªè¦åã¡ãã»ã¼ã¸ã表示ãããã ããã
Misplaced Views
Label - Header
Expected:x=152,y=28,width=160,height=44
Actual:x=150,y=25,width=158,height=37
ãã®è¦åã¯ãï¼ãã©ãã°ï¼ããããã§ï¼åæé ç½®ããã¨ãã®ãã¥ã¼ã®çµ¶å¯¾åº§æ¨ã¨ãAutolayoutã®å¶ç´ãããã¦ãã¾ãããã¨ããå 容ã§ãããHeaderãé¸æãã¦ãSize Inspectorã使ã£ã¦ãå¶ç´ã¨ä¸æ´åãèµ·ããªãããã«åº§æ¨ã¨ãµã¤ãºã調æ´ãã¦ããã°è§£æ±ºãããActualã絶対座æ¨ãªã®ã§ãAutolayoutã«åãããã«ã¯ã絶対座æ¨ãExpectedã®æ¹ã«åããã¦ããã
åæ§ã®æé ã§ä»åº¦ã¯ããã¿é¨ã®å¶ç´ãã»ããããã
ã»ããã¿é¨ã®ãã¥ã¼ãé¸æããã
ã»Pinã¢ã¤ã³ã³ãã¯ãªãã¯ããã
ã»å¶ç´ãè¨å®ãããã¤ã¢ãã°ãåºãã®ã§ã以ä¸ã®ããã«ããã
親ãã¥ã¼ã®ä¸ãã8pxãå·¦ãã8pxã®å¹
ãã»ãããï¼ä¸ã¨å·¦ã¸ã®ç¹ç·ã赤ãå®ç·ã«å¤ãã£ããã¨ã«æ³¨ç®ï¼
ãã®æç¹ã§ã¯ã¾ã ããã¿é¨ã®å¹
ã¨é«ããè¨å®ãã¦ããªãã
以ä¸ã®æé ã§ããã¿é¨ã®ãµã¤ãºããããé¨ã®ãµã¤ãºã«åããã¦ããã
ã»ãããé¨ã®ãã¥ã¼ã¨ããã¿é¨ã®ãã¥ã¼ã両æ¹é¸æãããï¼Command + Clickï¼
ã»Pinã¢ã¤ã³ã³ãã¯ãªãã¯ããã
ã»Equal Widthsã¨Equal Heightsã«ãã§ãã¯ãå
¥ãã¦Add 2 Constraintsãæ¼ãã
ã»ããã¿é¨ãé¸æãã¦ãSize Inspectorã§åº§æ¨ã¨ãµã¤ãºèª¿æ´ãè¡ãã
ï¼Expectedã®æ¹ã«åããã¦ãããã¨ï¼
æå¾ã«ã¡ã¤ã³ã³ã³ãã³ãé¨åã«å¶ç´ãã¤ããã
ã»ãããé¨ã®ãã¥ã¼ã¨ã¡ã¤ã³ã³ã³ãã³ãã®ãã¥ã¼ã両æ¹é¸æãããï¼Command + Clickï¼
ã»Alignã¢ã¤ã³ã³ãã¯ãªãã¯ããã
ã»Trailing Edgesã¨Top Edgesã«ãã§ãã¯ãå
¥ãã¦Add 2 Constraintsãæ¼ãã
Trailing Edgesã¯å³ç«¯ãåãããè¨å®ã§ãããTop Edgesã¯ä¸ç«¯ãåãããè¨å®ã§ãããããã®æç¹ã§ã¯ãã©ãããã©ãã¸ãã¨ããè¨å®ããã¾ããã£ã¦ããªããTrailing Edgesããä¿®æ£ãã¦ã¿ããã
ã»ç»é¢å·¦ã®ãã¥ã¼æ§é ãå±éãã¦ã
View Controller â View â Constraints å
ã®ç®çã®Trailing Alignmentãé¸æããã
ã»ç»é¢å³å´ã®Attributes Inspectorãéãã
ã»First Itemã«Main.Trailingãé¸æããã
ã»Second Itemã«Header.Trailingãé¸æããã
ï¼Firstã¨Secondãéã«ãªã£ã¦ããå ´åã¯ãReverse First And Second Itemãé¸æããï¼
ã»Constraintã®å¤ã«0ãã»ããããã
åæ§ã®æé ã§ãTop Alignmentã®å¶ç´ãä¿®æ£ããã
ã»ç»é¢å·¦ã®ãã¥ã¼æ§é ãå±éãã¦ã
View Controller â View â Constraints å
ã®ç®çã®Top Alignmentãé¸æããã
ã»ç»é¢å³å´ã®Attributes Inspectorãéãã
ã»First Itemã«Main.Topãé¸æããã
ã»Second Itemã«Header.Bottomãé¸æããã
ï¼Firstã¨Secondãéã«ãªã£ã¦ããå ´åã¯ãReverse First And Second Itemãé¸æããï¼
ã»Constraintã®å¤ã«8ãã»ããããã
ããã§ãããé¨ã¨ã¡ã¤ã³ã³ã³ãã³ãéã®å¶ç´ãã§ããã
ç¶ãã¦ãããã¿é¨ã¨ã¡ã¤ã³ã³ã³ãã³ãéã®å¶ç´ãå®ç¾©ãããä»åº¦ã¯ãã¡ã¤ã³ã³ã³ãã³ãé¨ã®Leadingï¼å·¦ç«¯ï¼ãããã¿é¨ã®Leadingï¼å·¦ç«¯ï¼ã«åããã¦ãããã«ã¡ã¤ã³ã³ã³ãã³ãé¨ã®ä¸é¨ãããã¿é¨ã®ä¸8ãã¯ã»ã«æåã¾ã§ã¨å®ç¾©ãããä½æ¥æé ã¯ããããé¨ã¨ã¡ã¤ã³ã³ã³ãã³ãéã®å¶ç´ãå®ç¾©ããã¨ãã¨ã»ã¨ãã©åãã§ããã
ã»ããã¿é¨ã®ãã¥ã¼ã¨ã¡ã¤ã³ã³ã³ãã³ãã®ãã¥ã¼ã両æ¹é¸æãããï¼Command + Clickï¼
ã»Alignã¢ã¤ã³ã³ãã¯ãªãã¯ããã
ã»Leading Edgesã¨Bottom Edgesã«ãã§ãã¯ãå
¥ãã¦Add 2 Constraintsãæ¼ãã
ã»ç»é¢å·¦ã®ãã¥ã¼æ§é ãå±éãã¦ã
View Controller â View â Constraints å
ã®ç®çã®Leading Alignmentãé¸æããã
ã»ç»é¢å³å´ã®Attributes Inspectorãéãã
ã»First Itemã«Main.Leadingãé¸æããã
ã»Second Itemã«Footer.Leadingãé¸æããã
ï¼Firstã¨Secondãéã«ãªã£ã¦ããå ´åã¯ãReverse First And Second Itemãé¸æããï¼
ã»Constraintã®å¤ã«0ãã»ããããã
ã»ç»é¢å·¦ã®ãã¥ã¼æ§é ãå±éãã¦ã
View Controller â View â Constraints å
ã®ç®çã®Bottom Alignmentãé¸æããã
ã»ç»é¢å³å´ã®Attributes Inspectorãéãã
ã»First Itemã«Main.Bottomãé¸æããã
ã»Second Itemã«Footer.Topãé¸æããã
ï¼Firstã¨Secondãéã«ãªã£ã¦ããå ´åã¯ãReverse First And Second Itemãé¸æããï¼
ã»Constraintã®å¤ã«-8ãã»ããããã
æå¾ã«ãè¦åãåç §ããªãããã¡ã¤ã³ã³ã³ãã³ãé¨ã®Size Inspectorãéãã¦ã座æ¨ã¨ãµã¤ãºã調æ´ããã
ããã§ããããå®æããã
Storyboardä¸ã§æå³ããéãã«ãããã«ã¬ã¤ã¢ã¦ãã§ãã¦ãããã¨ã確èªãã¦ã¿ãããæ©éã·ãã¥ã¬ã¼ã¿ã¼ã§å®è¡ãã¦ã¿ããã
ã4ã¤ã³ãç»é¢ - 横åãã
ã3.5ã¤ã³ãç»é¢ - 横åãã
ã ãã¶é¢åã ã£ãããã©ãã«ãæãéãã®ã¬ã¤ã¢ã¦ãã¯ã§ãããã ãããã®ç¨åº¦ã®ã¬ã¤ã¢ã¦ãã¯ã¾ã ã¾ã åºã®å£ã§ãå®éã®ã¨ãããAutolayoutã¯ãã£ã¨å¥¥æ·±ãã¨ããããè¬ããã¦ããã¨ããããç¹ã«ã¹ã¯ãã¼ã«ãã¥ã¼ã®Zoomï¼æ¡å¤§ç¸®å°ï¼ã¨ããã¤ã¹ã®å転ãéãªãã¨ã¨ãã§ããªãé¢åãªãã¨ã«ãªãããã®è¾ºã®è©±ã¯ãã¾ãå¥ã«æ©ä¼ã«ã§ãã
ããã§ã¯ã
楽ããæéã§ããï¼
ã¾ãã®æ©ä¼ãï¼