diff --git a/Example/Cartfile b/Example/Cartfile index ce636df..72d3802 100644 --- a/Example/Cartfile +++ b/Example/Cartfile @@ -1 +1 @@ -github "nodes-ios/KeyboardHelper" "master" +github "nodes-ios/KeyboardHelper" "swift-3.0" diff --git a/Example/Cartfile.resolved b/Example/Cartfile.resolved index 789ef8f..86a8ee8 100644 --- a/Example/Cartfile.resolved +++ b/Example/Cartfile.resolved @@ -1 +1 @@ -github "nodes-ios/KeyboardHelper" "30ed3728bf10b7d2a606d1e23bb23c12eea02222" +github "nodes-ios/KeyboardHelper" "58d7c2bf19f7bc93d733290f06ae0eedf03f18a5" diff --git a/Example/KeyboardHelperDemo.xcodeproj/project.pbxproj b/Example/KeyboardHelperDemo.xcodeproj/project.pbxproj index ac19532..476327e 100644 --- a/Example/KeyboardHelperDemo.xcodeproj/project.pbxproj +++ b/Example/KeyboardHelperDemo.xcodeproj/project.pbxproj @@ -128,6 +128,7 @@ TargetAttributes = { 29F30E661C807E2A00C77AF4 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0810; }; }; }; @@ -289,6 +290,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = dk.nodes.KeyboardHelperDemo; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -305,6 +307,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = dk.nodes.KeyboardHelperDemo; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/Example/KeyboardHelperDemo/AppDelegate.swift b/Example/KeyboardHelperDemo/AppDelegate.swift index 71b67b3..163cf7b 100644 --- a/Example/KeyboardHelperDemo/AppDelegate.swift +++ b/Example/KeyboardHelperDemo/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/Example/KeyboardHelperDemo/ViewController.swift b/Example/KeyboardHelperDemo/ViewController.swift index b6b18bd..862f164 100644 --- a/Example/KeyboardHelperDemo/ViewController.swift +++ b/Example/KeyboardHelperDemo/ViewController.swift @@ -11,8 +11,8 @@ import KeyboardHelper class ViewController: UIViewController, KeyboardNotificationDelegate { - private var tapGesture: UITapGestureRecognizer! - private var keyboardHelper : KeyboardHelper? + fileprivate var tapGesture: UITapGestureRecognizer! + fileprivate var keyboardHelper : KeyboardHelper? @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() { @@ -21,7 +21,7 @@ class ViewController: UIViewController, KeyboardNotificationDelegate { self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard)) self.view.addGestureRecognizer(tapGesture) - self.tapGesture.enabled = true + self.tapGesture.isEnabled = true self.keyboardHelper = KeyboardHelper(delegate: self) } @@ -30,7 +30,7 @@ class ViewController: UIViewController, KeyboardNotificationDelegate { self.view.endEditing(true) } - func keyboardWillAppear(info: KeyboardAppearanceInfo) { + func keyboardWillAppear(_ info: KeyboardAppearanceInfo) { info.animateAlong({ () -> Void in let insets = UIEdgeInsetsMake(0, 0, info.endFrame.size.height, 0) self.scrollView.contentInset = insets @@ -38,12 +38,12 @@ class ViewController: UIViewController, KeyboardNotificationDelegate { }) { finished in } } - func keyboardWillDisappear(info: KeyboardAppearanceInfo) { - UIView.animateWithDuration(NSTimeInterval(info.animationDuration), + func keyboardWillDisappear(_ info: KeyboardAppearanceInfo) { + UIView.animate(withDuration: TimeInterval(info.animationDuration), delay: 0, options: info.animationOptions, animations: { - let insets = UIEdgeInsetsZero + let insets = UIEdgeInsets.zero self.scrollView.contentInset = insets self.scrollView.scrollIndicatorInsets = insets }, diff --git a/KeyboardHelper.podspec b/KeyboardHelper.podspec index 6fb2605..acaf117 100644 --- a/KeyboardHelper.podspec +++ b/KeyboardHelper.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "KeyboardHelper" - s.version = "0.9.2" + s.version = "1.0.0" s.summary = "Handle UIKeyboard with ease, delegation and strongly typed user info included!" # This description is used to generate tags and improve search results. @@ -66,7 +66,7 @@ Pod::Spec.new do |s| # s.platform = :ios, "5.0" # When using multiple platforms - s.ios.deployment_target = "8.1" + s.ios.deployment_target = "8.0" # s.osx.deployment_target = "10.7" # s.watchos.deployment_target = "2.0" # s.tvos.deployment_target = "9.0" @@ -78,7 +78,7 @@ Pod::Spec.new do |s| # Supports git, hg, bzr, svn and HTTP. # - s.source = { :git => "https://github.com/nodes-ios/KeyboardHelper.git", :tag => "0.9.2" } + s.source = { :git => "https://github.com/nodes-ios/KeyboardHelper.git", :tag => s.version } # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # @@ -134,4 +134,3 @@ Pod::Spec.new do |s| # s.dependency "JSONKit", "~> 1.4" end - diff --git a/KeyboardHelper.xcodeproj/project.pbxproj b/KeyboardHelper.xcodeproj/project.pbxproj index 9dcabb6..63f0f48 100644 --- a/KeyboardHelper.xcodeproj/project.pbxproj +++ b/KeyboardHelper.xcodeproj/project.pbxproj @@ -184,7 +184,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0810; ORGANIZATIONNAME = Nodes; TargetAttributes = { 275BCA761C57C9F800FF3647 = { @@ -275,8 +275,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -322,8 +324,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -364,7 +368,7 @@ SKIP_INSTALL = YES; SWIFT_OBJC_INTERFACE_HEADER_NAME = "$(SWIFT_MODULE_NAME)-Swift.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -382,7 +386,8 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OBJC_INTERFACE_HEADER_NAME = "$(SWIFT_MODULE_NAME)-Swift.h"; - SWIFT_VERSION = 2.3; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -393,7 +398,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nodes.KeyboardHelperTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -404,7 +409,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nodes.KeyboardHelperTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/KeyboardHelper.xcodeproj/xcshareddata/xcschemes/KeyboardHelper.xcscheme b/KeyboardHelper.xcodeproj/xcshareddata/xcschemes/KeyboardHelper.xcscheme index 705b012..747eba5 100644 --- a/KeyboardHelper.xcodeproj/xcshareddata/xcschemes/KeyboardHelper.xcscheme +++ b/KeyboardHelper.xcodeproj/xcshareddata/xcschemes/KeyboardHelper.xcscheme @@ -1,6 +1,6 @@ Void, completion: (finished: Bool) -> Void) { - UIView.animateWithDuration( - animationDuration, + public func animateAlong(_ animationBlock: @escaping (() -> Void), completion: @escaping ((_ finished: Bool) -> Void) = { _ in }) { + UIView.animate( + withDuration: animationDuration, delay: 0.0, options: animationOptions, animations: animationBlock, completion: completion )} -} \ No newline at end of file +} diff --git a/KeyboardHelper/Classes/KeyboardHelper.swift b/KeyboardHelper/Classes/KeyboardHelper.swift index f1e0210..fb207ea 100644 --- a/KeyboardHelper/Classes/KeyboardHelper.swift +++ b/KeyboardHelper/Classes/KeyboardHelper.swift @@ -2,7 +2,7 @@ // KeyboardHelper.swift // KeyboardHelper // -// Created by Timmi Trinks on 27/01/16. +// Created by Kasper Welner on 27/01/16. // Copyright © 2016 Nodes. All rights reserved. // @@ -19,13 +19,13 @@ public protocol KeyboardNotificationDelegate: class { This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will appaear. - Parameter info: Struct `KeyboardAppearanceInfo`. */ - func keyboardWillAppear(info: KeyboardAppearanceInfo) + func keyboardWillAppear(_ info: KeyboardAppearanceInfo) /** This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will disappaear. - Parameter info: Struct `KeyboardAppearanceInfo`. */ - func keyboardWillDisappear(info: KeyboardAppearanceInfo) + func keyboardWillDisappear(_ info: KeyboardAppearanceInfo) } /** @@ -45,25 +45,25 @@ public class KeyboardHelper { required public init(delegate: KeyboardNotificationDelegate) { self.delegate = delegate - NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(KeyboardHelper.keyboardWillAppear(_:)), name: UIKeyboardWillShowNotification, object: nil) - NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(KeyboardHelper.keyboardWillDisappear(_:)), name: UIKeyboardWillHideNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(KeyboardHelper.keyboardWillAppear(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(KeyboardHelper.keyboardWillDisappear(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } private init() { delegate = nil } - dynamic private func keyboardWillAppear(note: NSNotification) { + dynamic private func keyboardWillAppear(_ note: Notification) { let info = KeyboardAppearanceInfo(notification: note) self.delegate?.keyboardWillAppear(info) } - dynamic private func keyboardWillDisappear(note: NSNotification) { + dynamic private func keyboardWillDisappear(_ note: Notification) { let info = KeyboardAppearanceInfo(notification: note) self.delegate?.keyboardWillDisappear(info) } deinit { - NSNotificationCenter.defaultCenter().removeObserver(self) + NotificationCenter.default.removeObserver(self) } } diff --git a/KeyboardHelperTests/Tests/KeyboardAppearanceInfoTests.swift b/KeyboardHelperTests/Tests/KeyboardAppearanceInfoTests.swift index ee1c57a..3f732cd 100644 --- a/KeyboardHelperTests/Tests/KeyboardAppearanceInfoTests.swift +++ b/KeyboardHelperTests/Tests/KeyboardAppearanceInfoTests.swift @@ -11,18 +11,18 @@ import KeyboardHelper class KeyboardAppearanceInfoTests: XCTestCase { - var apperanceInfo: KeyboardAppearanceInfo! + var appearanceInfo: KeyboardAppearanceInfo! var defaultsAppearanceInfo : KeyboardAppearanceInfo! override func setUp() { super.setUp() // Create test info - var testUserInfo: [String: AnyObject] = [ - UIKeyboardFrameBeginUserInfoKey: NSValue(CGRect: CGRect(x: 100, y: 100, width: 100, height: 100)), - UIKeyboardFrameEndUserInfoKey: NSValue(CGRect: CGRect(x: 200, y: 200, width: 200, height: 200)), + var testUserInfo: [String: Any] = [ + UIKeyboardFrameBeginUserInfoKey: NSValue(cgRect: CGRect(x: 100, y: 100, width: 100, height: 100)), + UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: CGRect(x: 200, y: 200, width: 200, height: 200)), UIKeyboardAnimationDurationUserInfoKey: 3.0, - UIKeyboardAnimationCurveUserInfoKey: UIViewAnimationCurve.EaseOut.rawValue, + UIKeyboardAnimationCurveUserInfoKey: NSNumber(integerLiteral: UIViewAnimationCurve.easeOut.rawValue), ] if #available(iOS 9.0, *) { @@ -32,52 +32,53 @@ class KeyboardAppearanceInfoTests: XCTestCase { } // Fake the notification - let note = NSNotification(name: UIKeyboardWillShowNotification, object: nil, userInfo: testUserInfo) - apperanceInfo = KeyboardAppearanceInfo(notification: note) - let defaultNote = NSNotification(name: UIKeyboardWillShowNotification, object: nil, userInfo: nil) + let note = Notification(name: NSNotification.Name.UIKeyboardWillShow, object: nil, userInfo: testUserInfo) + appearanceInfo = KeyboardAppearanceInfo(notification: note) + let defaultNote = Notification(name: NSNotification.Name.UIKeyboardWillShow, object: nil, userInfo: nil) defaultsAppearanceInfo = KeyboardAppearanceInfo(notification: defaultNote) } func testBeginFrame() { - XCTAssertEqual(apperanceInfo.beginFrame, CGRect(x: 100, y: 100, width: 100, height: 100), + XCTAssertEqual(appearanceInfo.beginFrame, CGRect(x: 100, y: 100, width: 100, height: 100), "Parsing beginFrame from keyboard appearance info failed.") - XCTAssertEqual(defaultsAppearanceInfo.beginFrame, CGRectZero, + XCTAssertEqual(defaultsAppearanceInfo.beginFrame, CGRect.zero, "Parsing default beginFrame from keyboard appearance info failed.") } func testEndFrame() { - XCTAssertEqual(apperanceInfo.endFrame, CGRect(x: 200, y: 200, width: 200, height: 200), + XCTAssertEqual(appearanceInfo.endFrame, CGRect(x: 200, y: 200, width: 200, height: 200), "Parsing endFrame from keyboard appearance info failed.") - XCTAssertEqual(defaultsAppearanceInfo.endFrame, CGRectZero, + XCTAssertEqual(defaultsAppearanceInfo.endFrame, CGRect.zero, "Parsing default endFrame from keyboard appearance info failed.") } @available(iOS 9.0, *) func testBelongsToCurrentApp() { - XCTAssertEqual(apperanceInfo.belongsToCurrentApp, false, + XCTAssertEqual(appearanceInfo.belongsToCurrentApp, false, "Parsing belongsToCurrentApp from keyboard appearance info failed.") XCTAssertEqual(defaultsAppearanceInfo.belongsToCurrentApp, true, "Parsing default belongsToCurrentApp from keyboard appearance info failed.") } func testAnimationDuration() { - XCTAssertEqual(apperanceInfo.animationDuration, Double(3), + + XCTAssertEqual(appearanceInfo.animationDuration, Double(3), "Parsing animationDuration from keyboard appearance info failed.") XCTAssertEqual(defaultsAppearanceInfo.animationDuration, Double(0.25), "Parsing default animationDuration from keyboard appearance info failed.") } func testAnimationCurve() { - XCTAssertEqual(apperanceInfo.animationCurve, UIViewAnimationCurve(rawValue: 2), + XCTAssertEqual(appearanceInfo.animationCurve, UIViewAnimationCurve(rawValue: 2), "Parsing animationCurve from keyboard appearance info failed.") XCTAssertEqual(defaultsAppearanceInfo.animationCurve, UIViewAnimationCurve(rawValue: defaultsAppearanceInfo.animationCurve.rawValue), "Parsing default animationCurve from keyboard appearance info failed.") } func testAnimateAlong() { - let expectation = expectationWithDescription("Animate along should take 3 seconds") + let expectation = self.expectation(description: "Animate along should take 3 seconds") - apperanceInfo.animateAlong({ () -> Void in + appearanceInfo.animateAlong({ () -> Void in // Do animations }) { (finished) -> Void in if finished { @@ -85,7 +86,7 @@ class KeyboardAppearanceInfoTests: XCTestCase { } } - waitForExpectationsWithTimeout(3.005, handler: nil) + waitForExpectations(timeout: 3.005, handler: nil) } } diff --git a/KeyboardHelperTests/Tests/KeyboardHelperTests.swift b/KeyboardHelperTests/Tests/KeyboardHelperTests.swift index 759685d..1d24763 100644 --- a/KeyboardHelperTests/Tests/KeyboardHelperTests.swift +++ b/KeyboardHelperTests/Tests/KeyboardHelperTests.swift @@ -14,11 +14,11 @@ class ShowSpyDelegate : KeyboardNotificationDelegate { var expectation : XCTestExpectation? - func keyboardWillDisappear(info: KeyboardAppearanceInfo) { + func keyboardWillDisappear(_ info: KeyboardAppearanceInfo) { } - func keyboardWillAppear(info: KeyboardAppearanceInfo) { + func keyboardWillAppear(_ info: KeyboardAppearanceInfo) { guard let expectation = expectation else { XCTFail("ShowSpyDelegate was not setup correctly. Missing XCTExpectation reference") return @@ -34,7 +34,7 @@ class HideSpyDelegate : KeyboardNotificationDelegate { var expectation : XCTestExpectation? - func keyboardWillDisappear(info: KeyboardAppearanceInfo) { + func keyboardWillDisappear(_ info: KeyboardAppearanceInfo) { guard let expectation = expectation else { XCTFail("HideSpyDelegate was not setup correctly. Missing XCTExpectation reference") return @@ -44,7 +44,7 @@ class HideSpyDelegate : KeyboardNotificationDelegate { expectation.fulfill() } - func keyboardWillAppear(info: KeyboardAppearanceInfo) { + func keyboardWillAppear(_ info: KeyboardAppearanceInfo) { } } @@ -55,33 +55,33 @@ class KeyboardHelperTests: XCTestCase { let spyDelegate = ShowSpyDelegate() let kh = KeyboardHelper(delegate: spyDelegate) - let expectation = expectationWithDescription("KeyboardHelper calls the delegate as the result of receiving the show notification") + let expectation = self.expectation(description: "KeyboardHelper calls the delegate as the result of receiving the show notification") spyDelegate.expectation = expectation // NSNotificationCenter.defaultCenter().postNotificationName(UIKeyboardWillShowNotification, object: kh) - let notification : NSNotification + let notification : Notification if #available(iOS 9.0, *) { - notification = NSNotification(name: UIKeyboardWillShowNotification, object: kh, userInfo:[ - UIKeyboardAnimationCurveUserInfoKey : NSNumber(int: 7), - UIKeyboardAnimationDurationUserInfoKey : NSNumber(double: 0.25), - UIKeyboardFrameBeginUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 667, width: 375, height: 0)), - UIKeyboardFrameEndUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 409, width: 375, height: 258)), - UIKeyboardIsLocalUserInfoKey : NSNumber(bool: true) + notification = Notification(name: NSNotification.Name.UIKeyboardWillShow, object: kh, userInfo:[ + UIKeyboardAnimationCurveUserInfoKey : NSNumber(value: 7), + UIKeyboardAnimationDurationUserInfoKey : NSNumber(value: 0.25), + UIKeyboardFrameBeginUserInfoKey : NSValue(cgRect: CGRect(x: 0, y: 667, width: 375, height: 0)), + UIKeyboardFrameEndUserInfoKey : NSValue(cgRect: CGRect(x: 0, y: 409, width: 375, height: 258)), + UIKeyboardIsLocalUserInfoKey : NSNumber(value: true) ]) } else { - notification = NSNotification(name: UIKeyboardWillShowNotification, object: nil, userInfo:[ - UIKeyboardAnimationCurveUserInfoKey : NSNumber(int: 7), - UIKeyboardAnimationDurationUserInfoKey : NSNumber(double: 0.25), - UIKeyboardFrameBeginUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 667, width: 375, height: 0)), - UIKeyboardFrameEndUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 409, width: 375, height: 258)) + notification = Notification(name: NSNotification.Name.UIKeyboardWillShow, object: nil, userInfo:[ + UIKeyboardAnimationCurveUserInfoKey : NSNumber(value: 7), + UIKeyboardAnimationDurationUserInfoKey : NSNumber(value: 0.25), + UIKeyboardFrameBeginUserInfoKey : NSValue(cgRect: CGRect(x: 0, y: 667, width: 375, height: 0)), + UIKeyboardFrameEndUserInfoKey : NSValue(cgRect: CGRect(x: 0, y: 409, width: 375, height: 258)) ]) } - NSNotificationCenter.defaultCenter().postNotification(notification) + NotificationCenter.default.post(notification) - waitForExpectationsWithTimeout(1) { error in + waitForExpectations(timeout: 1) { error in if let error = error { XCTFail("waitForExpectationsWithTimeout errored: \(error)") } @@ -104,13 +104,13 @@ class KeyboardHelperTests: XCTestCase { let spyDelegate = HideSpyDelegate() let kh = KeyboardHelper(delegate: spyDelegate) - let expectation = expectationWithDescription("KeyboardHelper calls the delegate as the result of receiving the hide notification") + let expectation = self.expectation(description: "KeyboardHelper calls the delegate as the result of receiving the hide notification") spyDelegate.expectation = expectation - NSNotificationCenter.defaultCenter().postNotificationName(UIKeyboardWillHideNotification, object: kh) + NotificationCenter.default.post(name: NSNotification.Name.UIKeyboardWillHide, object: kh) - waitForExpectationsWithTimeout(1) { error in + waitForExpectations(timeout: 1) { error in if let error = error { XCTFail("waitForExpectationsWithTimeout errored: \(error)") } diff --git a/README.md b/README.md index b6574fd..26ab920 100644 --- a/README.md +++ b/README.md @@ -15,30 +15,23 @@ A small (but cool) tool for handling UIKeyboard appearing and disappearing in yo ## 📦 Installation ### Carthage +~~~bash +github "nodes-ios/KeyboardHelper" ~> 1.0 ~~~ -github "nodes-ios/KeyboardHelper" -~~~ + +> Last versions compatible with lower Swift versions: +> +> **Swift 2.3** +> `github "nodes-ios/KeyboardHelper" == 0.10.0` +> +> **Swift 2.2** +> `github "nodes-ios/KeyboardHelper" == 0.9.4` ### CocoaPods ~~~ -pod 'KeyboardHelper', '~> 0.9' +pod 'KeyboardHelper', '~> 1.0' ~~~ -### Swit Package Manager -To use KeyboardHelper as a [Swift Package Manager](https://swift.org/package-manager/) package just add the following to your `Package.swift` file. - -~~~swift -import PackageDescription - -let package = Package( - name: "YourPackage", - dependencies: [ - .Package(url: "https://github.com/nodes-ios/KeyboardHelper.git", majorVersion: 0) - ] -) -~~~ - -**NOTE:** This doesn't currently work as SPM doesn't support iOS, but once it will we will already be supporting it! :) ## 🔧 Setup Implement `KeyboardNotificationDelegate` in your UIViewController. @@ -47,18 +40,22 @@ Implement `KeyboardNotificationDelegate` in your UIViewController. class ViewController: UIViewController, KeyboardNotificationDelegate ``` -Add a `KeyboardHelper` private variable and initialize it, setting the delegate. +Add a `KeyboardHelper` private variable, initialize it and set the delegate. ```swift private var keyboardHelper : KeyboardHelper? -... -self.keyboardHelper = KeyboardHelper(delegate: self) + +func viewDidLoad() { + ... + self.keyboardHelper = KeyboardHelper(delegate: self) + ... +} ``` Implement the two methods in the `KeyboardNotificationDelegate`: ```swift -public func keyboardWillAppear(info: KeyboardHelper.KeyboardAppearanceInfo) -public func keyboardWillDisappear(info: KeyboardHelper.KeyboardAppearanceInfo) +public func keyboardWillAppear(_ info: KeyboardHelper.KeyboardAppearanceInfo) +public func keyboardWillDisappear(_ info: KeyboardHelper.KeyboardAppearanceInfo) ``` Both methods take as argument a `KeyboardAppearanceInfo` object, which is basically a wrapper over the `userInfo` dictionary of the `UIKeyboardWillShowNotification` and `UIKeyboardWillHideNotification` notifications. @@ -66,8 +63,8 @@ Both methods take as argument a `KeyboardAppearanceInfo` object, which is basica One example of implementation for the two delegate methods is: ```swift -func keyboardWillAppear(info: KeyboardAppearanceInfo) { - UIView.animateWithDuration(NSTimeInterval(info.animationDuration), +func keyboardWillAppear(_ info: KeyboardAppearanceInfo) { + UIView.animate(withDuration: TimeInterval(info.animationDuration), delay: 0, options: info.animationOptions, animations: { @@ -78,17 +75,17 @@ func keyboardWillAppear(info: KeyboardAppearanceInfo) { completion:nil) } - func keyboardWillDisappear(info: KeyboardAppearanceInfo) { - UIView.animateWithDuration(NSTimeInterval(info.animationDuration), - delay: 0, - options: info.animationOptions, - animations: { - let insets = UIEdgeInsetsZero - self.scrollView.contentInset = insets - self.scrollView.scrollIndicatorInsets = insets - }, - completion:nil) - } +func keyboardWillDisappear(_ info: KeyboardAppearanceInfo) { + UIView.animate(withDuration: TimeInterval(info.animationDuration), + delay: 0, + options: info.animationOptions, + animations: { + let insets = UIEdgeInsetsZero + self.scrollView.contentInset = insets + self.scrollView.scrollIndicatorInsets = insets + }, + completion:nil) +} ``` The `KeyboardAppearanceInfo` object has the following properties: