-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #closes #477 * Fix tests
- Loading branch information
1 parent
faf7e4e
commit 20ca06c
Showing
5 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// UIWindowExtensions.swift | ||
// SwifterSwift | ||
// | ||
// Created by Omar Albeik on 6/2/18. | ||
// Copyright © 2018 SwifterSwift | ||
// | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
#if os(iOS) | ||
public extension UIWindow { | ||
|
||
/// SwifterSwift: Switch current root view controller with a new view controller. | ||
/// | ||
/// - Parameters: | ||
/// - viewController: new view controller. | ||
/// - animated: set to true to animate view controller change (default is true). | ||
/// - duration: animation duration in seconds (default is 0.5). | ||
/// - options: animataion options (default is .transitionFlipFromRight). | ||
/// - completion: optional completion handler called after view controller is changed. | ||
public func switchRootViewController( | ||
to viewController: UIViewController, | ||
animated: Bool = true, | ||
duration: TimeInterval = 0.5, | ||
options: UIViewAnimationOptions = .transitionFlipFromRight, | ||
_ completion: (() -> Void)? = nil) { | ||
|
||
guard animated else { | ||
rootViewController = viewController | ||
completion?() | ||
return | ||
} | ||
|
||
UIView.transition(with: self, duration: duration, options: options, animations: { | ||
let oldState = UIView.areAnimationsEnabled | ||
UIView.setAnimationsEnabled(false) | ||
self.rootViewController = viewController | ||
UIView.setAnimationsEnabled(oldState) | ||
}, completion: { _ in | ||
completion?() | ||
}) | ||
} | ||
|
||
} | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// UIWindowExtensionsTests.swift | ||
// SwifterSwift | ||
// | ||
// Created by Omar Albeik on 6/2/18. | ||
// Copyright © 2018 SwifterSwift | ||
// | ||
|
||
#if os(iOS) | ||
import XCTest | ||
@testable import SwifterSwift | ||
|
||
final class UIWindowExtensionsTests: XCTestCase { | ||
|
||
func testSwitchRootViewController() { | ||
let viewController = UIViewController() | ||
let tableViewController = UITableViewController() | ||
|
||
let window = UIWindow() | ||
window.rootViewController = viewController | ||
|
||
XCTAssertNotNil(window.rootViewController) | ||
XCTAssertEqual(window.rootViewController!, viewController) | ||
|
||
window.switchRootViewController(to: tableViewController, animated: false) | ||
XCTAssertNotNil(window.rootViewController) | ||
XCTAssertEqual(window.rootViewController!, tableViewController) | ||
|
||
let completionExpectation = expectation(description: "Completed") | ||
|
||
window.switchRootViewController(to: viewController, animated: true, duration: 0.75) { | ||
completionExpectation.fulfill() | ||
XCTAssertNotNil(window.rootViewController) | ||
XCTAssertEqual(window.rootViewController!, viewController) | ||
} | ||
|
||
waitForExpectations(timeout: 1, handler: nil) | ||
} | ||
|
||
} | ||
#endif |