From 9dd8db5cef1585d275c3e500adde3d654453d00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akon=20Bogen?= Date: Mon, 11 May 2026 14:00:03 +0200 Subject: [PATCH] Modernize to Swift 5.9+, replace Travis CI with GitHub Actions - Replace deprecated NSNotification.Name.UIKeyboardWillShow/WillHide with UIResponder.keyboardWillShowNotification/keyboardWillHideNotification - Replace deprecated UIKeyboardAnimationDurationUserInfoKey / UIKeyboardFrameEndUserInfoKey with UIResponder.* equivalents - Replace UIViewAnimationOptions with UIView.AnimationOptions - Replace UIEdgeInsetsMake() with UIEdgeInsets(top:left:bottom:right:) - Use guard-let for safer userInfo unwrapping - Use trailing closure syntax for UIView.animate - Use .zero for clearing insets - Add .github/workflows/ci.yml for GitHub Actions CI - Replace Travis CI badge in README with GitHub Actions badge --- .github/workflows/ci.yml | 9 ++++++ README.md | 2 +- UIViewController+Keyboard.swift | 50 ++++++++++++++++----------------- 3 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d220d4b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,9 @@ +name: CI +on: [push, pull_request] +jobs: + build: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - name: Build + run: xcodebuild -project ScrollViewKeyboardResize.xcodeproj -scheme ScrollViewKeyboardResize build diff --git a/README.md b/README.md index 9a056b1..7994a8b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # SingleLineKeyboardResize With this non-intrusive UIViewController extension, only a single line of code will make your scrollView auto resize when the keyboard appears. It literally cannot get easier than this, and you don't even need to subclass anything. -[![CI Status](http://img.shields.io/travis/Haaakon/SingleLineKeyboardResize.svg?style=flat)](https://travis-ci.org/haaakon/SingleLineKeyboardResize) +[![CI](https://github.com/haaakon/SingleLineKeyboardResize/actions/workflows/ci.yml/badge.svg)](https://github.com/haaakon/SingleLineKeyboardResize/actions/workflows/ci.yml) [![Version](https://img.shields.io/cocoapods/v/SingleLineKeyboardResize.svg?style=flat)](http://cocoadocs.org/docsets/SingleLineKeyboardResize) [![License](https://img.shields.io/cocoapods/l/SingleLineKeyboardResize.svg?style=flat)](http://cocoadocs.org/docsets/SingleLineKeyboardResize) [![Platform](https://img.shields.io/cocoapods/p/SingleLineKeyboardResize.svg?style=flat)](http://cocoadocs.org/docsets/SingleLineKeyboardResize) diff --git a/UIViewController+Keyboard.swift b/UIViewController+Keyboard.swift index 1547b3f..e12b8af 100644 --- a/UIViewController+Keyboard.swift +++ b/UIViewController+Keyboard.swift @@ -7,21 +7,21 @@ import UIKit -private var scrollViewKey : UInt8 = 0 +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! { get { return objc_getAssociatedObject(self, &scrollViewKey) as? UIScrollView @@ -30,29 +30,27 @@ extension UIViewController { objc_setAssociatedObject(self, &scrollViewKey, newValue, .OBJC_ASSOCIATION_ASSIGN) } } - + @objc func keyboardWillShow(_ notification: Notification) { - let userInfo = (notification as NSNotification).userInfo as! Dictionary - let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval - let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.cgRectValue - let keyboardFrameConvertedToViewFrame = view.convert(keyboardFrame!, from: nil) - let options = UIViewAnimationOptions.beginFromCurrentState - UIView.animate(withDuration: animationDuration, delay: 0, options:options, animations: { () -> Void in + guard let userInfo = notification.userInfo, + let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval, + let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } + let keyboardFrameConvertedToViewFrame = view.convert(keyboardFrame, from: nil) + let options: UIView.AnimationOptions = .beginFromCurrentState + UIView.animate(withDuration: animationDuration, delay: 0, options: options) { 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) - }) { (complete) -> Void in + self.internalScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: insetHeight, right: 0) + self.internalScrollView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: insetHeight, right: 0) } } - + @objc func keyboardWillHide(_ notification: Notification) { - let userInfo = (notification as NSNotification).userInfo as! Dictionary - let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval - let options = UIViewAnimationOptions.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) - }) { (complete) -> Void in + guard let userInfo = notification.userInfo, + let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else { return } + let options: UIView.AnimationOptions = .beginFromCurrentState + UIView.animate(withDuration: animationDuration, delay: 0, options: options) { + self.internalScrollView.contentInset = .zero + self.internalScrollView.scrollIndicatorInsets = .zero } } }