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
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
50 changes: 24 additions & 26 deletions UIViewController+Keyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, AnyObject>
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<String, AnyObject>
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
}
}
}
Loading