안녕하세요. iOSangBong입니다.
오늘은 safeArea의 길이를 구해보도록 하겠습니다.
SwiftUI에서는 ignoresSafeArea나 edgesIgnoringSafeArea를 해주면
간단하게 SafeArea를 고려할 수 있지만
프로젝트에서는 활용하기가 힘든 점이 많이 있습니다.
그래서 따로 safeArea의 길이를 알 수 있다면
그만큼 쉽게 원하는데로 View를 구성할 수 있겠죠?
일단 아래의 사진은 SafeArea에 대한 설정을 하지 않은 View 입니다.
위와 같이 SafeArea는 흰색으로 나타나게 됩니다.
비교를 위해서 왼쪽은 ignoresSafeArea를 해주고 오른쪽은 해주지 않겠습니다.
그러면 이런식으로 나타나게 됩니다.
이제 왼쪽 View를 오른쪽 View와 동일하게 만들어주겠습니다.
1. UIWindow 추출하기
일단 현재 화면에 대한 UIWindow를 가져와야 합니다.
UIApplication을 확장해서 connectedScenes를 활용해줍니다.
2. SafeAreaInset 만들기
추출한 UIWindow를 바탕으로 safeAreaInsets를 구할 수 있는 방법입니다.
EnvironmentValues에 safeAreaInsets 포함시켜주면 됩니다.
이런식으로 한다음에 바로 사용을 해보겠습니다.
3. safeAreaInsets 사용
@Environment로 safeAreaInsets를 선언해준 다음
왼쪽에 있는 View에 상하 safeArea 높이만큼을 padding을 줬습니다.
그 결과는?
좌우가 동일하게 나오는걸 보실 수 있습니다.
이걸 이용해서 프로젝트에 다양하게 적용하실 수 있을 겁니다.
프로젝트에 잘 활용해보세요~
참고는 깃허브를 참고해주세요~
https://github.com/bongbong9708/BongStorage
다들 즐코딩하세요. 그럼 이만~
'SwiftUI > 2. 잡기술' 카테고리의 다른 글
[iOS][SwiftUI] 키보드 간단하게 내리기 (0) | 2023.08.21 |
---|---|
[iOS][SwiftUI] Rectangle 특정 모서리 둥글게 만들기 (0) | 2023.08.07 |
[iOS][SwiftUI] @focusState를 Binding 하기 (0) | 2023.08.02 |
[iOS][SwiftUI] Modal, fullScreenCover 배경 투명하게 만들기 (0) | 2023.07.15 |
[iOS][SwiftUI] Modal, fullScreenCover 애니메이션 없애기 (0) | 2023.07.13 |