iPhone 14 Pro, iPhone 14 Pro Maxでは「dynamic island」と呼ばれる余白領域が新たに生じている。
USE YOUR LOAF - iPhone 14 Screen Sizes
単純にステータスバーの高さのビューを追加しているだけだと余白が出てしまう。そのため、UIWindowのsafeAreaInsets.top
により余白の部分を含んだ高さを取得し、その高さのビューをUIWindowに追加し、そのビューの背景色を変えるなどすれば対応できる。
UIApplication+StatusBar.swift
extension UIApplication {
var statusBarView: UIView? {
if #available(iOS 13.0, *) {
let tag = 3848245
let keyWindow = UIApplication.shared.connectedScenes
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows.first
if let statusBar = keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let topPadding = keyWindow?.safeAreaInsets.top
let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: topPadding ?? 0.0))
statusBarView.tag = tag
statusBarView.layer.zPosition = 999999
keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}
func setStatusBarColor(with color: UIColor = UIColor(hex: "#3269D8FF")!) {
statusBarView?.backgroundColor = color
}
}
参考:
Stack Over Flow - How to change the status bar background color and text color on iOS 13?
Apple Developer Forum - Space under status bar(dynamic island) in iPhone 14 Pros