develop

frame과 bounds의 차이 본문

iOS

frame과 bounds의 차이

pikachu987 2021. 7. 11. 13:01
반응형

frame과 bounds의 차이는

 

1. frame은 x, y가 super 뷰 기준의 좌표계로 설정되고

bounds는 x, y좌표가 자기 자신 기준의 좌표계로 설정된다.

let testView = UIView(frame: CGRect(x: 0, y: 100, width: 80, height: 80))
testView.backgroundColor = .blue
view.addSubview(testView)

print("frame: \(testView.frame)")
print("bounds: \(testView.bounds)")
frame: (0.0, 100.0, 80.0, 80.0)
bounds: (0.0, 0.0, 80.0, 80.0)

 

2. frame을 변경하게 되면 자기 자신의 좌표가 이동된다.

bounds을 변경하게 되면 자기 자신은 그대로이고 subview들이 반대로 이동하게 되는것 처럼 보인다.

let testView = UIView(frame: CGRect(x: 0, y: 100, width: 80, height: 80))
testView.backgroundColor = .blue
view.addSubview(testView)

let testSubview = UIView(frame: CGRect(x: 10, y: 10, width: 20, height: 20))
testSubview.backgroundColor = .red
testView.addSubview(testSubview)

먼저 frame을 변경해 보겠다.

let testView = UIView(frame: CGRect(x: 0, y: 100, width: 80, height: 80))
testView.backgroundColor = .blue
view.addSubview(testView)

let testSubview = UIView(frame: CGRect(x: 10, y: 10, width: 20, height: 20))
testSubview.backgroundColor = .red
testView.addSubview(testSubview)

testView.frame = CGRect(x: 6, y: 160, width: 100, height: 100)

print("testView.frame: \(testView.frame)")
print("testView.bounds: \(testView.bounds)")
print("testSubview.frame: \(testSubview.frame)")
print("testSubview.bounds: \(testSubview.bounds)")
testView.frame: (6.0, 160.0, 100.0, 100.0)
testView.bounds: (0.0, 0.0, 100.0, 100.0)
testSubview.frame: (10.0, 10.0, 20.0, 20.0)
testSubview.bounds: (0.0, 0.0, 20.0, 20.0)

 

이제 bounds를 변경해 보겠다.

let testView = UIView(frame: CGRect(x: 0, y: 100, width: 80, height: 80))
testView.backgroundColor = .blue
view.addSubview(testView)

let testSubview = UIView(frame: CGRect(x: 10, y: 10, width: 20, height: 20))
testSubview.backgroundColor = .red
testView.addSubview(testSubview)

testView.bounds = CGRect(x: -60, y: -60, width: 100, height: 100)

print("testView.frame: \(testView.frame)")
print("testView.bounds: \(testView.bounds)")
print("testSubview.frame: \(testSubview.frame)")
print("testSubview.bounds: \(testSubview.bounds)")
testView.frame: (-10.0, 90.0, 100.0, 100.0)
testView.bounds: (-60.0, -60.0, 100.0, 100.0)
testSubview.frame: (10.0, 10.0, 20.0, 20.0)
testSubview.bounds: (0.0, 0.0, 20.0, 20.0)

bounds의 x, y좌표를 음수로 주니 실제 보이는 화면에서는 subview가 우측 하단으로 가게 된다.

양수로 주면 좌측 상단으로 가게 된다.

 

 

3. frame는 회전을 하였을때 좌표가 회전한 값이 (소수점까지) 출력이 된다.

bounds는 회전을 하였을때 좌표가 회전한 상태와 상관없이 출력이 된다.

let testView = UIView(frame: CGRect(x: 0, y: 100, width: 80, height: 80))
testView.backgroundColor = .blue
view.addSubview(testView)

let testSubview = UIView(frame: CGRect(x: 10, y: 10, width: 20, height: 20))
testSubview.backgroundColor = .red
testView.addSubview(testSubview)

testView.transform = CGAffineTransform(rotationAngle: 1.12312312)

print("testView.frame: \(testView.frame)")
print("testView.bounds: \(testView.bounds)")
testView.frame: (-13.373037726121076, 86.62696227387892, 106.74607545224217, 106.74607545224217)
testView.bounds: (0.0, 0.0, 80.0, 80.0)

 

 

 

 

 

 

 

반응형

'iOS' 카테고리의 다른 글

ARC, 순환참조, 캡쳐  (0) 2021.07.13
hugging, compression resistance에 대해서  (0) 2021.07.12
Hashable에 대해  (0) 2021.06.23
==과 ===의 차이, closure의 ===  (0) 2021.06.22
iOS에서 srt, smi 파일 한글 깨지는 문제  (0) 2021.03.01
Comments