Skip to content

Commit

Permalink
Merge pull request #76 from devxoul/ipad-orientation
Browse files Browse the repository at this point in the history
Fix iPad orientation
  • Loading branch information
devxoul committed Apr 11, 2016
2 parents 701745e + 2d91fb4 commit 63f741e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
30 changes: 11 additions & 19 deletions JLToast/JLToastView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down
42 changes: 34 additions & 8 deletions JLToast/JLToastWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 63f741e

Please sign in to comment.