From bc6575e9dbae8409ab355d356e869ddac5b8e475 Mon Sep 17 00:00:00 2001 From: Felix Mau Date: Sun, 27 Mar 2022 09:46:35 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20Improve=20safe=20area?= =?UTF-8?q?=20device=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NotchGradientLoadingBarController.swift | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/GradientLoadingBar/Classes/NotchGradientLoadingBarController.swift b/GradientLoadingBar/Classes/NotchGradientLoadingBarController.swift index dc47ce4..13672cd 100644 --- a/GradientLoadingBar/Classes/NotchGradientLoadingBarController.swift +++ b/GradientLoadingBar/Classes/NotchGradientLoadingBarController.swift @@ -43,21 +43,7 @@ open class NotchGradientLoadingBarController: GradientLoadingBarController { // MARK: - Public methods override open func setupConstraints(superview: UIView) { - guard #available(iOS 11.0, *) else { - /// The notch is only available when supporting safe area layout guides, which is starting from iOS 11. - super.setupConstraints(superview: superview) - return - } - - // The `safeAreaInsets.top` always includes the status-bar and therefore will always be greater "0". - // As a workaround we check the bottom inset. - let hasNotch = superview.safeAreaInsets.bottom > 0 - - // The iPad also has a bottom inset, in order to avoid the home indicator. (https://developer.apple.com/forums/thread/110724) - // As a workaround we explicitly make sure to have a phone device. - let isIPhone = UIDevice.current.userInterfaceIdiom == .phone - - guard hasNotch, isIPhone else { + guard let notchConfig = viewModel.safeAreaDevice.notchConfig else { // No special masking required for non safe area devices. super.setupConstraints(superview: superview) return @@ -65,8 +51,7 @@ open class NotchGradientLoadingBarController: GradientLoadingBarController { // As we currently only support portrait mode (and no device rotation), we can safely use `bounds.size.width` here. let screenWidth = superview.bounds.size.width - - let notchBezierPath = self.notchBezierPath(for: screenWidth, notchConfig: viewModel.safeAreaDevice.notchConfig) + let notchBezierPath = self.notchBezierPath(for: screenWidth, notchConfig: notchConfig) // Setting the `lineWidth` draws a line, where the actual path is exactly in the middle of the drawn line. // To get the correct height (including the path) we have to add the `height` here to the given bounds (half height for top, half for bottom). @@ -230,9 +215,12 @@ open class NotchGradientLoadingBarController: GradientLoadingBarController { private extension NotchGradientLoadingBarViewModel.SafeAreaDevice { /// The notch specific configuration for the current device. - var notchConfig: NotchConfig { + var notchConfig: NotchConfig? { switch self { - case .unknown, .iPhoneX, .iPhone11: + case .unknown: + return nil + + case .iPhoneX, .iPhone11: /// The default configuration for the iPhone X and 11. /// Values are based on . return NotchConfig(notchWidth: 208,