Reputation: 5842
How to detect in Swift if the current device (iPhone) has a physical home button or hasn't, like: iPhone X, iPhone Xs, iPhone Xs Max, iPhone Xr ?
Upvotes: 16
Views: 12502
Reputation: 2331
iOS version >= 13.0
UIApplication.shared.windows is deprecated from iOS 15.0, we can replace it like below:
var hasHomeButton: Bool {
let window = UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
guard let safeAreaBottom = window?.safeAreaInsets.bottom else {
return false
}
return safeAreaBottom <= 0
}
In SwiftUI you can also use this variable, but don't use it from init()
of your SwiftUI view struct.
Upvotes: 0
Reputation: 377
For iOS 13 and higher:
var isBottom: Bool {
if #available(iOS 13.0, *), UIApplication.shared.windows[0].safeAreaInsets.bottom > 0 {
return true
}
return false
}
Upvotes: 6
Reputation: 13753
Check the safe area:
if @available(iOS 11.0, *),
UIApplication.sharedApplication.keyWindow?.safeAreaInsets.bottom > 0 {
return true
}
return false
Swift 4.2 version:-
var isBottom: Bool {
if #available(iOS 11.0, *), let keyWindow = UIApplication.shared.keyWindow, keyWindow.safeAreaInsets.bottom > 0 {
return true
}
return false
}
You can also check the device type (check out this post), but checking the safe area is probably the easiest way.
Upvotes: 36