Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions ScrollViewKeyboardResize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@
TargetAttributes = {
EC687AFA1A8C938400BD1E6A = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1010;
};
EC687B111A8C938400BD1E6A = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1010;
TestTargetID = EC687AFA1A8C938400BD1E6A;
};
};
Expand Down Expand Up @@ -378,7 +378,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "ScrollViewKeyboardResize/ScrollViewKeyboardResize-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -392,7 +393,8 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.haaakon.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "ScrollViewKeyboardResize/ScrollViewKeyboardResize-Bridging-Header.h";
SWIFT_VERSION = 4.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand All @@ -412,7 +414,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.haaakon.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ScrollViewKeyboardResize.app/ScrollViewKeyboardResize";
};
name = Debug;
Expand All @@ -429,7 +432,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.haaakon.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ScrollViewKeyboardResize.app/ScrollViewKeyboardResize";
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion ScrollViewKeyboardResize/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Expand Down
26 changes: 13 additions & 13 deletions UIViewController+Keyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ private var scrollViewKey : UInt8 = 0
extension UIViewController {

public func setupKeyboardNotifcationListenerForScrollView(_ scrollView: UIScrollView) {
NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
internalScrollView = scrollView
}

public func removeKeyboardNotificationListeners() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}

fileprivate var internalScrollView: UIScrollView! {
Expand All @@ -33,25 +33,25 @@ extension UIViewController {

@objc func keyboardWillShow(_ notification: Notification) {
let userInfo = (notification as NSNotification).userInfo as! Dictionary<String, AnyObject>
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.cgRectValue
let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey]?.cgRectValue
let keyboardFrameConvertedToViewFrame = view.convert(keyboardFrame!, from: nil)
let options = UIViewAnimationOptions.beginFromCurrentState
let options = UIView.AnimationOptions.beginFromCurrentState
UIView.animate(withDuration: animationDuration, delay: 0, options:options, animations: { () -> Void in
let insetHeight = (self.internalScrollView.frame.height + self.internalScrollView.frame.origin.y) - keyboardFrameConvertedToViewFrame.origin.y
self.internalScrollView.contentInset = UIEdgeInsetsMake(0, 0, insetHeight, 0)
self.internalScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, insetHeight, 0)
self.internalScrollView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: insetHeight, right: 0)
self.internalScrollView.scrollIndicatorInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: insetHeight, right: 0)
}) { (complete) -> Void in
}
}

@objc func keyboardWillHide(_ notification: Notification) {
let userInfo = (notification as NSNotification).userInfo as! Dictionary<String, AnyObject>
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
let options = UIViewAnimationOptions.beginFromCurrentState
let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
let options = UIView.AnimationOptions.beginFromCurrentState
UIView.animate(withDuration: animationDuration, delay: 0, options:options, animations: { () -> Void in
self.internalScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
self.internalScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.internalScrollView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
self.internalScrollView.scrollIndicatorInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
}) { (complete) -> Void in
}
}
Expand Down