Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed wrong banner dimensions due to the wrong orientation detection #431

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 56 additions & 17 deletions NotificationBanner/Classes/UIWindow+orientation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,66 @@ import UIKit
extension UIWindow {

public var width: CGFloat {
let orientation = UIDevice.current.orientation
switch orientation {
case .landscapeLeft, .landscapeRight:
return max(frame.width, frame.height)
case .portrait, .portraitUpsideDown:
return min(frame.width, frame.height)
default:
return frame.width
if #available(iOS 13.0, *) {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
let orientation = UIApplication.shared.supportedInterfaceOrientations(for: scene.windows.first)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why supportedInterfaceOrientations is being checked here. Most of the time, if landscape is supported, then both .landscapeLeft and .landscapeRight would be in the mask returned. In that case, the switch would just fall through to the default of using frame.width. The same goes if both portrait and landscape orientations are supported. Shouldn't frame.width just be used all the time instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prioritizes supported orientation over the current device orientation state, for example when the app is locked in portrait, and the device is in the landscape, it returns the portrait which will result in a correct orientation. I'm not sure if it's the best way to do it but just double-checked it and it works like a charm! Please feel free to upgrade it though!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that supportedInterfaceOrientations should take priority over UIDevice.current.orientation. My point was that I think that simply checking window.frame.width or window.frame.height will take the app's current orientation (which is restricted by supported orientations) into account without needing to check for the app's supported orientations separately.

switch orientation {
case .landscapeLeft, .landscapeRight:
return max(frame.width, frame.height)
case .portrait, .portraitUpsideDown:
return min(frame.width, frame.height)
default:
return frame.width
}
} else {
return getDefaultWidth()
}
} else {
return getDefaultWidth()
}

func getDefaultWidth() -> CGFloat {
let orientation = UIDevice.current.orientation
switch orientation {
case .landscapeLeft, .landscapeRight:
return max(frame.width, frame.height)
case .portrait, .portraitUpsideDown:
return min(frame.width, frame.height)
default:
return frame.width
}
}
}

public var height: CGFloat {
let orientation = UIDevice.current.orientation
switch orientation {
case .landscapeLeft, .landscapeRight:
return min(frame.width, frame.height)
case .portrait, .portraitUpsideDown:
return max(frame.width, frame.height)
default:
return frame.height
if #available(iOS 13.0, *) {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
let orientation = UIApplication.shared.supportedInterfaceOrientations(for: scene.windows.first)
switch orientation {
case .landscapeLeft, .landscapeRight:
return min(frame.width, frame.height)
case .portrait, .portraitUpsideDown:
return max(frame.width, frame.height)
default:
return frame.height
}
} else {
return getDefaultHeight()
}
} else {
return getDefaultHeight()
}
}

func getDefaultHeight() -> CGFloat {
let orientation = UIDevice.current.orientation
switch orientation {
case .landscapeLeft, .landscapeRight:
return min(frame.width, frame.height)
case .portrait, .portraitUpsideDown:
return max(frame.width, frame.height)
default:
return frame.height
}
}
}
}