์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- UIKit

(iOS) - UIKit์˜ Frame, Bounds ์˜ ์ฐจ์ด์ 

  • -

๊ณตํ†ต์ 

Bounds, Frame ๋ชจ๋‘ View์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” UIView์˜ instance property์ธ CGRect ํƒ€์ž…์ด๋ผ๋Š” ์ ์ด ๊ณตํ†ต์ ์ด๋‹ค.

var frame: CGRect {get set}
var bounds: CGRect {get set}

// Frame property ์ ‘๊ทผ
view.frame.origin.x
view.frame.origin.y
view.frame.size.width
view.frame.size.height

view.bounds.origin.x
view.bounds.origin.y
view.bounds.size.width
view.bounds.size.height

 

 

Frame (Instance Property)

(๋ถ„๋ฅ˜: UIKit > Views and Controls > UIView)

  • ํ”„๋ ˆ์ž„์€ SuperView's coordinate system ์•ˆ์—์„œ View์˜ ์‚ฌ์ด์ฆˆ ๋ฐ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. (superView๋Š” ๋ฐ”๋กœ ํ•œ๋‹จ๊ณ„ ์ƒ์œ„ ๋ทฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.)
  • frame์˜ size๋Š” ๋‹จ์ˆœํžˆ UIView์™€ ๋™์ผํ•œ ์œ„์น˜, ํฌ๊ธฐ๊ฐ€ ์•„๋‹ˆ๋ผ View๊ฐ€ ํšŒ์ „๋˜์—ˆ์„๋•Œ๋„ ๊ฐ์Œ€ ์ˆ˜ ์žˆ๋Š” ์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. (์ฆ‰ View๋ฅผ ํšŒ์ „์‹œํ‚ค๋ฉด ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋‹ค)
  • Bounds์˜ ์ขŒํ‘œ๋ฅผ ์˜ฎ๊ธฐ๋ฉด subView๊ฐ€ ์›€์ง์ด๋Š”๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ด๋Š”๋ฐ, ์ด ๊ฒƒ์€ subview๋ฅผ ๊ทธ๋ฆฌ๋Š” ์ขŒํ‘œ๊ณ„์˜ ๊ธฐ์ค€์ด ๋‹ฌ๋ผ์กŒ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

Bounds (Instance Property)

๋ถ„๋ฅ˜: UIKit > Views and Controls > UIView

  • Bounds๋Š”in its own coordinate system ์ž๊ธฐ ์ž์‹ ์˜ ์ขŒํ‘œ๊ณ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์œ„์น˜์™€ ์‚ฌ์ด์ฆˆ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” property์ด๋‹ค.
  • SuperView.bounds ์ขŒํ‘œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด, subView๊ฐ€ ์›€์ง์ธ๋‹ค.
  • Bounds์˜ size๋Š” ๋‹จ์ˆœํžˆ ๋ทฐ ์ž์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. (๋ทฐ๋ฅผ ํšŒ์ „ํ•ด๋„ size๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.)

 

 

์š”์•ฝ

  • Frame์€ ์ƒ์œ„ ๋ทฐ์˜ ์ขŒํ‘œ์ฒด๊ณ„๋ฅผ ๋”ฐ๋ฅธ๋‹ค.
    • ๋”ฐ๋ผ์„œ ์ผ๋ฐ˜์ ์œผ๋กœ UIView์˜ ์œ„์น˜, ํฌ๊ธฐ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.
  • Bounds๋Š” ์ž๊ธฐ ์ž์‹ ๋งŒ์˜ ์ขŒํ‘œ์ฒด๊ณ„๋ฅผ ๊ฐ€์ง„๋‹ค.
    • ๋‚ด๋ถ€์˜ ํ•˜์œ„๋ทฐ๋“ค์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.
    • ๋‚ด๋ถ€์— ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค(drawRect)

์ฆ‰, SuperView.bounds์˜ origin๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด SuperView ์ž๊ธฐ ์ž์‹ ์˜ ์ขŒํ‘œ๊ณ„๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค.

-> Subview๋Š” ์ƒ์œ„ ๋ทฐ์ธ SuperView์˜ ์ขŒํ‘œ์ฒด๊ณ„์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค.

-> SubView์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€๋‹ค.

์ฆ‰ ์ƒ์œ„๋ทฐ(blue) bounds.origin ์„ (x+100,y+100)๋กœ ์ˆ˜์ •ํ–ˆ๋”๋‹ˆ....

ํ•˜์œ„๋ทฐ์˜(red)๊ฐ€ ๊ฐ๊ฐ (x-100, y-100)๋งŒํผ ์ด๋™ํ•œ๊ฒƒ์ฒ˜๋Ÿผ ๋ Œ๋”๋ง๋œ๋‹ค.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.