안전한 지역 삽입 상하 높이 확보
새로운 iPhone X에서 안전하지 않은 지역의 상단 및 하단 높이를 얻는 가장 적절한 방법은 무엇입니까?
이 시도 :
에서 목표 C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
에서 스위프트
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
레이아웃 가이드 사이의 높이를 얻으려면 방금 수행하십시오.
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
그래서 full frame height = 812.0
,safe area height = 734.0
아래는 녹색 뷰의 프레임이있는 예입니다 guide.layoutFrame
스위프트 4
제약 조건은 장면 뒤의 자동 레이아웃 API에 의해 처리되므로 제약 조건을 사용하여 뷰를 안전 영역 앵커에 고정하는 것은 뷰 컨트롤러 수명주기의 어느 시점에서나 수행 할 수 있습니다. 그러나 높이를 결정하기 위해 해당 안전 영역의 값에 액세스하려면 뷰 컨트롤러의 수명주기 후반까지 이상적으로 기다려야합니다 viewDidLayoutSubviews()
.
이것은 모든 뷰 컨트롤러에 연결됩니다.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var topSafeArea: CGFloat
var bottomSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = view.safeAreaInsets.top
bottomSafeArea = view.safeAreaInsets.bottom
} else {
topSafeArea = topLayoutGuide.length
bottomSafeArea = bottomLayoutGuide.length
}
// safe area values are now available to use
}
I prefer this method to getting it off of the window because it’s how Apple designed the API to be used. Don't reference extraneous objects like the window when everything is already in the view controller. And by using viewDidLayoutSubviews()
, safe area values are updated during orientation changes, double-height status bars, and all other mutations to the view and its safe areas.
None of the other answers here worked for me, but this did.
var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.windows[0]
let safeFrame = window.safeAreaLayoutGuide.layoutFrame
topSafeAreaHeight = safeFrame.minY
bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
}
I'm working with CocoaPods frameworks and in case UIApplication.shared
is unavailable then I use safeAreaInsets
in view's window
:
if #available(iOS 11.0, *) {
let insets = view.window?.safeAreaInsets
let top = insets.top
let bottom = insets.bottom
}
Objective-C Who had the problem when keyWindow is equal to nil. Just put the code above in viewDidAppear (not in viewDidLoad)
safeAreaLayoutGuide 보기가 화면에 표시되면이 안내서는 탐색 막대, 탭 막대, 도구 모음 및 기타 조상보기로 다루지 않는보기 부분을 반영합니다. tvOS에서 안전 영역은 화면의 베젤을 덮지 않은 영역을 반영합니다.보기가 현재보기 계층 구조로 설치되어 있지 않거나 화면에 아직 표시되지 않으면 레이아웃 안내선 가장자리는보기의 가장자리와 같습니다.
그런 다음 스크린 샷에서 빨간색 화살표의 높이를 얻으려면 다음과 같습니다.
self.safeAreaLayoutGuide.layoutFrame.size.height
참고 URL : https://stackoverflow.com/questions/46829840/get-safe-area-inset-top-and-bottom-heights
'programing tip' 카테고리의 다른 글
PHP를 사용하여 CSV 파일을 구문 분석하는 방법 (0) | 2020.07.13 |
---|---|
log4net이 작동하지 않습니다 (0) | 2020.07.13 |
Gradle 빌드 유형별로 앱 이름을 변경하는 방법 (0) | 2020.07.13 |
AppCompat 라이브러리에서 SwitchCompat의 색상을 변경하는 방법 (0) | 2020.07.13 |
Rails 콘솔에서 멋진 포맷을 얻는 방법 (0) | 2020.07.13 |