Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] safeArea 크기 구하는 방법(#'keyWindow' was deprecated in iOS 13.0, #safeAreaInsets) 본문

iOS 응용 (swift)

[iOS - swift] safeArea 크기 구하는 방법(#'keyWindow' was deprecated in iOS 13.0, #safeAreaInsets)

jake-kim 2024. 4. 8. 01:21

safe area 경고 표출

  • 보통 아래처럼 keyWindow로 구하면 warning 문구가 표출
// 'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes
let window = UIApplication.shared.keyWindow
let safeAreaInsets = window?.safeAreaInsets
  • iOS 13부터 Scene이라는 개념이 생겼는데, 이 scene에 접근해서 window를 가져와야함

safeAreaInset 구하는 방법

  • connectedScenes에 접근하여 첫번째 scene 가져오기
let connectedScene = UIApplication.shared.connectedScenes.first
  • UIWindowScene으로 캐스팅
let windowScene = connectedScene as? UIWindowScene
  • 첫번째 window 가져오기
let window = windowScene?.windows.first
  • safeAreaInset 획득
let safeAreaInset = window?.safeAreaInsets ?? .zero
print(safeAreaInset) // UIEdgeInsets(top: 59.0, left: 0.0, bottom: 34.0, right: 0.0)
  • 고차함수를 사용하여 표현
let safeAreaInset = UIApplication.shared.connectedScenes.first
    .flatMap { ($0 as? UIWindowScene)?.windows.first }?
    .flatMap { $0.safeAreaInsets }
Comments