ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

ios-og


๐Ÿ“ฑ Auto Layout - Anatomy of a Constraint


๐ŸŽฏ Auto Layout์ด๋ž€?

  • Auto Layout Guide์˜ ์ •์˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views.

    ์˜คํ†  ๋ ˆ์ด์•„์›ƒ์€ ๋ทฐ์— ์ •์˜๋œ ์ œ์•ฝ๋“ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ทฐ ๊ณ„์ธต์—์„œ ๋ชจ๋“  ๋ทฐ๋“ค์˜ ํฌ๊ธฐ์™€ ์œ„์น˜๋ฅผ ๋™์ ์œผ๋กœ ๊ณ„์‚ฐํ•ด์ค๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    For example, you can constrain a button so that it is horizontally centered with an Image view and so that the buttonโ€™s top edge always remains 8 points below the imageโ€™s bottom. If the image viewโ€™s size or position changes, the buttonโ€™s position automatically adjusts to match.

    ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด, ๋ฒ„ํŠผ์— ์ œ์•ฝ์„ ์ค„ ๋•Œ ๋ฒ„ํŠผ์˜ ๊ฐ€์žฅ ์œ—๋ถ€๋ถ„์ด ๋ฒ„ํŠผ ์ƒ๋‹จ์— ์กด์žฌํ•˜๋Š” ์ด๋ฏธ์ง€์˜ ๊ฐ€์žฅ ์•„๋ž˜์ชฝ์—์„œ๋ถ€ํ„ฐ 8 ํฌ์ธํŠธ ๋–จ์–ด์ง€๊ฒŒ ์„ค์ •ํ•œ๋‹ค๋ฉด View์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ฐ”๋€Œ๊ฑฐ๋‚˜(์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค๋ฅธ ๊ธฐ๊ธฐ) ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ฒ„ํŠผ์˜ ์œ„์น˜๊ฐ€ ์ž๋™์œผ๋กœ ์žฌ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.

    This constraint-based approach to design allows you to build user interfaces that dynamically respond to both internal and external changes.

    ๊ฒฐ๊ตญ ์˜คํ†  ๋ ˆ์ด์•„์›ƒ์€ ๋ณ€ํ™”์— ๋Œ€ํ•ด ๋™์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ๋ฐ˜์‘ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์ค€์ ์„ ์ค‘์‹ฌ์œผ๋กœ ๊ฑฐ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ ˆ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์š”์†Œ๊ฐ„์˜ ์ƒ๋Œ€์  ์œ„์น˜ ์ œ์•ฝ์„ ํ†ตํ•ด ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ์ •ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.


๐Ÿ—œ Constraint(์ œ์•ฝ) ์„ค์ •

  • ์•„๋ž˜๋Š” ๋‘ ์š”์†Œ Blue, Red ๊ฐ„์— ๊ฑฐ๋ฆฌ 8์ธ ์ œ์•ฝ์„ ์ฃผ๋Š” ๊ทธ๋ฆผ์ž…๋‹ˆ๋‹ค.

    RedView์™€ BlueView๋Š” ๊ฐ๊ฐ ์ œ์•ฝ์„ ๋ฐ›์„ ์š”์†Œ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ๋’ค์˜ Leading์€ ๊ทธ ์š”์†Œ์˜ ์‹œ์ž‘์ (leading edge), trailing์€ ์š”์†Œ Blue์˜ ๋ ์ (trailing edge)์„ ์˜๋ฏธํ•˜๊ณ  ๊ทธ ๊ฑฐ๋ฆฌ๋ฅผ Constant 8.0์œผ๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • Constraints๋ฅผ ์ค„ ๋•Œ์— ํ•„์š”ํ•œ ๊ธฐ์ค€๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์—ฌ๊ธฐ์„œ ์™ผ์ชฝ์ด Leading, ์˜ค๋ฅธ์ชฝ์ด Trailing์ธ ์ด์œ ๋Š” ๊ธ€์„ ์ฝ๋Š” ๋ฐฉํ–ฅ์ด ์˜์–ด ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. RTL(Right to Left)์— ๋”ฐ๋ผ ์™ผ์ชฝ ๋ฐฉํ–ฅ์„ Leading์œผ๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

    ์œ„์˜ attributes๋Š” ํฌ๊ฒŒ ๋‘๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    size attributes: Width, Height

    location attributes: Top, Bottom... etc.

    size attributes๋Š” ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์†์„ฑ์ด๋ฉฐ location attributes๋Š” ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

    • size attribute์™€ location attribute๋Š” ํ•จ๊ป˜ ์“ธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
    • location attributes์— ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. size attributes์—๋Š” ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ location attributes๋Š” ์œ„์น˜๋ฅผ ๊ธฐ๋ฐ˜ํ•œ ์ƒ๋Œ€์  ์„ ์–ธ์ด๋ฏ€๋กœ ์ƒ์ˆ˜๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
    • location attributes์—์„œ๋Š” multiplier๋ฅผ 1.0์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • location attributes์—์„œ vertical attributes(์ˆ˜์ง)์™€ horizontal attributes(์ˆ˜ํ‰)๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐฉํ–ฅ์ด ์„œ๋กœ 90๋„๋กœ ์ œ์•ฝ์„ ์ค„ ์ˆ˜ ์—†๋Š” ๊ด€๊ณ„์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    • Leading/Trailing attributes์™€ Left/Right attributes๋ฅผ ์„œ๋กœ ํ˜ผ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. Leading์€ Trailing๊ณผ, Left๋Š” Right๊ณผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    ๐Ÿ“ Contraints ์˜ˆ์‹œ


    ์•„๋ž˜๋Š” contraints์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

    1. NotAnAttribute๋Š” ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ์˜จ์ „ํžˆ ์˜ค๋ฅธ์ชฝ์˜ Constant(์˜ˆ์‹œ์—์„œ 40.0)๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๋ผ๋ฉด 0์œผ๋กœ ์ƒ๊ฐํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    2. Button_1์˜ ์˜ค๋ฅธ์ชฝ ๋์—์„œ 8.0 ๋–จ์–ด์ง„ ๊ณณ์— Button_2์˜ ์™ผ์ชฝ ๋์„ ๋ฐฐ์น˜ํ•˜๊ฒ ๋‹ค๋Š” location attributes ์ž…๋‹ˆ๋‹ค.
    3. allign์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์•„๋ž˜๋กœ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ๋•Œ ๊ฐ™์€ ๋ผ์ธ์— ์กด์žฌํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ Button_1๊ณผ Button_2์˜ ์™ผ์ชฝ edge๊ฐ€ ๊ฐ™์€ ์„ ์ƒ์— ์กด์žฌํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
    4. ๋‘ ๋ฒ„ํŠผ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ ๊ฐ™๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    5. ์ƒ์œ„ ๋ทฐ์™€ ๊ทธ ๋ทฐ์— ์กด์žฌํ•˜๋Š” ๋‹ค๋ฅธ ๋ทฐ๊ฐ€ ์žˆ์„ ๋•Œ, ์ƒ์œ„ ๋ทฐ์˜ ์ • ๊ฐ€์šด๋ฐ์— ํ•˜์œ„ ๋ทฐ๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š” attributes์ž…๋‹ˆ๋‹ค.
    6. ๊ฐ€๋กœ ์„ธ๋กœ ๋น„์œจ์„ 1:2๋กœ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

    ์—ฌ๊ธฐ์„œ Equal(=)์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ• ๋‹น์˜ ๊ฐœ๋…์ด ์•„๋‹™๋‹ˆ๋‹ค. ํ• ๋‹น ์‹œ์—๋Š” ์–‘์ชฝ์˜ ๊ฐ’์ด ๋‹ฌ๋ผ๋„ ์ƒ๊ด€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ attributes์—์„œ equal์€ ์ˆ˜ํ•™์ ์œผ๋กœ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ์ด๋ฉฐ ์–‘์ชฝ์˜ ๊ฐ’์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.


๐Ÿ’ฌ Constraints Inequalities & Priorities


  • Contraints๋Š” ๊ผญ equal๋กœ ์„ค์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

    ํŠน์ • ์ œ์•ฝ ์กฐ๊ฑด ํ•˜์—์„œ ๊ทธ ์‚ฌ์ด ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์„ ๋•Œ ์œ„์ฒ˜๋Ÿผ ๋ถ€๋“ฑํ˜ธ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์ง€๋งŒ equal ์ •์˜๋ณด๋‹ค ๋งŽ์€ ์ƒ๊ฐ์„ ํ•ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Contraints์—๋Š” ์šฐ์„ ๋„(Priorities)๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„ ๋„๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ์ถฉ๋Œํ•˜๋Š” Constraints๊ฐ€ ์žˆ๋‹ค๋ฉด ์šฐ์„ ๋„๊ฐ€ ๋†’์€ Constraints๋งŒ ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„ ๋„๊ฐ€ ๋†’์€ Constraint๊ฐ€ ๋จผ์ € ์ž‘๋™ํ•˜๊ณ , ๊ทธ ์ดํ›„ ์šฐ์„ ๋„์˜ Constraint๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

    ์šฐ์„ ๋„๋Š” 1์—์„œ 1000 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ–์Šต๋‹ˆ๋‹ค.


๐Ÿ—œ Intrinsic Content Size(๊ณ ์œ  ์ปจํ…์ธ  ์‚ฌ์ด์ฆˆ)

  • Constraint๋Š” View์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ œ์•ฝ์„ ์ค„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ปจํ…์ธ  ๊ณ ์œ ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ํ†ตํ•ด ์ œ์•ฝ์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฒ„ํŠผ์˜ ๊ณ ์œ  ์‚ฌ์ด์ฆˆ๋Š” ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ํ…์ŠคํŠธ(title)์˜ ํฌ๊ธฐ์— ์•ฝ๊ฐ„์˜ ๋งˆ์ง„๊ฐ’์„ ๋”ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    So far, all of the examples have used constraints to define both the viewโ€™s position and its size. However, some views have a natural size given their current content. This is referred to as their intrinsic content size. For example, a buttonโ€™s intrinsic content size is the size of its title plus a small margin.

    Label ๋“ฑ์˜ ์š”์†Œ๋Š” ๊ณ ์œ  ์‚ฌ์ด์ฆˆ๊ฐ€ ์ •ํ•ด์ ธ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑํ•œ ํ›„ ๋ฐ”๋กœ ์œ„์น˜์— ๋Œ€ํ•œ constraints๋ฅผ ์ค˜๋„ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ UIView, NSView์˜ ๊ฒฝ์šฐ๋Š” ๊ณ ์œ  ์‚ฌ์ด์ฆˆ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ ํ›„ ์‚ฌ์ด์ฆˆ ์ •์˜ ์—†์ด ๋ฐ”๋กœ ์œ„์น˜์— ๋Œ€ํ•œ constraints๋ฅผ ์ฃผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Text/image views๋Š” ๊ณ ์œ  ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Slider์˜ ๊ฒฝ์šฐ iOS์—์„œ ๋†’์ด๋Š” ๊ณ ์ •๋˜์–ด์žˆ๊ณ  ๋„ˆ๋น„๋งŒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ƒ์„ฑํ•œ ํ›„ ์œ„์น˜์— ๋Œ€ํ•œ constraints๋ฅผ ์ฃผ๋ฉด ๋†’์ด์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ ๋„ˆ๋น„๊ฐ€ ํ™•์ •๋˜์–ด ์žˆ์ง€ ์•Š์•„ ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์˜ constraints์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ—œ CHCR(Contents Compression, Contents Hugging)

  • Contents Compression: ์ปจํ…์ธ ๊ฐ€ ์ค„์–ด๋“ค์ง€ ์•Š์œผ๋ ค๊ณ  ๋ฒ„ํ‹ฐ๋Š” ํž˜์ž…๋‹ˆ๋‹ค.

  • Contents Hugging: ์ปจํ…์ธ ๊ฐ€ ๋Š˜์–ด๋‚˜์ง€ ์•Š์œผ๋ ค๊ณ  ๋ฒ„ํ‹ฐ๋Š” ํž˜์ž…๋‹ˆ๋‹ค.

    ์ค„์–ด๋“œ๋ ค๊ณ  ํ•˜๋Š” ํž˜์ด๋‚˜ ๋Š˜์–ด๋‚˜๋ ค๋Š” ํž˜์œผ๋กœ ์ฐฉ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด ๋‘ ๊ฐœ๋…์€ ๋ฒ„ํ‹ฐ๋Š” ๋ฐ˜๋ฐœ๋ ฅ์— ๊ฐ€๊นŒ์šด ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ ์—ญ์‹œ Priority๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์šฐ์„ ๋„๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

    Label์˜ ๊ฒฝ์šฐ contents๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก ๊ทธ ๊ณ ์œ  ํฌ๊ธฐ๊ฐ€ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์€ Label์— ์„œ๋กœ ์˜ํ–ฅ์„ ์ฃผ๋Š” Constraints๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Content Hugging์„ ์„ค์ •ํ•ด์ฃผ๋ฉด ํฌ๊ธฐ๊ฐ€ ๋Š˜์–ด๋‚˜๋Š”๋ฐ ํ•œ๊ณ„๊ฐ€ ์ƒ๊ธฐ๋ฉฐ, ๋ฐ˜๋Œ€๋กœ Content compression์„ ์„ค์ •ํ•˜๋ฉด ๋ ˆ์ด๋ธ”์ด ๋„ˆ๋ฌด ์ž‘์•„์ง€๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Reference: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/index.html

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG more
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ