From 2d91fb4280619b47456ee2ceb077243e15c5e3d1 Mon Sep 17 00:00:00 2001 From: Suyeol Jeon Date: Tue, 12 Apr 2016 02:23:20 +0900 Subject: [PATCH] Fix iPad orientation --- JLToast/JLToastView.swift | 30 ++++++++++---------------- JLToast/JLToastWindow.swift | 42 ++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/JLToast/JLToastView.swift b/JLToast/JLToastView.swift index 1eb405a..a83ce01 100644 --- a/JLToast/JLToastView.swift +++ b/JLToast/JLToastView.swift @@ -79,8 +79,8 @@ public let JLToastViewLandscapeOffsetYAttributeName = "JLToastViewLandscapeOffse } func updateView() { - let deviceWidth = CGRectGetWidth(UIScreen.mainScreen().bounds) - let constraintSize = CGSize(width: deviceWidth * (280.0 / 320.0), height: CGFloat.max) + let containerSize = JLToastWindow.sharedWindow.frame.size + let constraintSize = CGSize(width: containerSize.width * (280.0 / 320.0), height: CGFloat.max) let textLabelSize = self.textLabel.sizeThatFits(constraintSize) self.textLabel.frame = CGRect( x: self.textInsets.left, @@ -100,12 +100,6 @@ public let JLToastViewLandscapeOffsetYAttributeName = "JLToastViewLandscapeOffse var width:CGFloat var height:CGFloat - let screenSize = UIScreen.mainScreen().bounds.size - let backgroundViewSize = self.backgroundView.frame.size - - let orientation = UIApplication.sharedApplication().statusBarOrientation - let systemVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue - let userInterfaceIdiom = UIDevice.currentDevice().userInterfaceIdiom let portraitOffsetY = self.dynamicType.defaultValueForAttributeName( JLToastViewPortraitOffsetYAttributeName, @@ -116,20 +110,18 @@ public let JLToastViewLandscapeOffsetYAttributeName = "JLToastViewLandscapeOffse forUserInterfaceIdiom: userInterfaceIdiom ) as! CGFloat - if UIInterfaceOrientationIsLandscape(orientation) && systemVersion < 8.0 { - width = screenSize.height - height = screenSize.width - y = landscapeOffsetY + let orientation = UIApplication.sharedApplication().statusBarOrientation + if orientation.isPortrait || !JLToastWindow.sharedWindow.shouldRotateManually { + width = containerSize.width + height = containerSize.height + y = portraitOffsetY } else { - width = screenSize.width - height = screenSize.height - if UIInterfaceOrientationIsLandscape(orientation) { - y = landscapeOffsetY - } else { - y = portraitOffsetY - } + width = containerSize.height + height = containerSize.width + y = landscapeOffsetY } + let backgroundViewSize = self.backgroundView.frame.size x = (width - backgroundViewSize.width) * 0.5 y = height - (backgroundViewSize.height + y) self.frame = CGRect(x: x, y: y, width: backgroundViewSize.width, height: backgroundViewSize.height); diff --git a/JLToast/JLToastWindow.swift b/JLToast/JLToastWindow.swift index 3a9ff41..bee1024 100644 --- a/JLToast/JLToastWindow.swift +++ b/JLToast/JLToastWindow.swift @@ -26,6 +26,14 @@ public class JLToastWindow: UIWindow { /// Will not return `rootViewController` while this value is `true`. Or the rotation will be fucked in iOS 9. var isStatusBarOrientationChanging = false + /// Don't rotate manually if the device is iPad with iOS 9. + var shouldRotateManually: Bool { + if #available(iOS 9, *), UIDevice.currentDevice().userInterfaceIdiom == .Pad { + return false + } + return true + } + override public var rootViewController: UIViewController? { get { guard !self.isStatusBarOrientationChanging else { return nil } @@ -57,6 +65,11 @@ public class JLToastWindow: UIWindow { name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil ) + NSNotificationCenter.defaultCenter().addObserver(self, + selector: #selector(self.applicationDidBecomeActive), + name: UIApplicationDidBecomeActiveNotification, + object: nil + ) } required public init?(coder aDecoder: NSCoder) { @@ -81,19 +94,32 @@ public class JLToastWindow: UIWindow { self.isStatusBarOrientationChanging = false } + func applicationDidBecomeActive() { + let orientation = UIApplication.sharedApplication().statusBarOrientation + self.handleRotate(orientation) + } + func handleRotate(orientation: UIInterfaceOrientation) { let angle = self.angleForOrientation(orientation) - self.transform = CGAffineTransformMakeRotation(CGFloat(angle)) - - if orientation.isPortrait { - self.frame.size.width = UIScreen.mainScreen().bounds.size.width - self.frame.size.height = UIScreen.mainScreen().bounds.size.height - } else { - self.frame.size.width = UIScreen.mainScreen().bounds.size.height - self.frame.size.height = UIScreen.mainScreen().bounds.size.width + if self.shouldRotateManually { + self.transform = CGAffineTransformMakeRotation(CGFloat(angle)) + } + + if let window = UIApplication.sharedApplication().windows.first { + if orientation.isPortrait || !self.shouldRotateManually { + self.frame.size.width = window.bounds.size.width + self.frame.size.height = window.bounds.size.height + } else { + self.frame.size.width = window.bounds.size.height + self.frame.size.height = window.bounds.size.width + } } self.frame.origin = .zero + + dispatch_async(dispatch_get_main_queue()) { + JLToastCenter.defaultCenter().currentToast?.view.updateView() + } } func angleForOrientation(orientation: UIInterfaceOrientation) -> Double {