├── .github ├── with-keyboardavoiding.png └── without-keyboardavoiding.png ├── Tests ├── LinuxMain.swift └── KeyboardAvoidingTests │ ├── XCTestManifests.swift │ └── KeyboardAvoidingTests.swift ├── README.md ├── LICENSE ├── Package.swift ├── .gitignore └── Sources └── KeyboardAvoiding └── KeyboardAvoiding.swift /.github/with-keyboardavoiding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a2/KeyboardAvoiding/HEAD/.github/with-keyboardavoiding.png -------------------------------------------------------------------------------- /.github/without-keyboardavoiding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a2/KeyboardAvoiding/HEAD/.github/without-keyboardavoiding.png -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import KeyboardAvoidingTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += KeyboardAvoidingTests.allTests() 7 | XCTMain(tests) 8 | -------------------------------------------------------------------------------- /Tests/KeyboardAvoidingTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(KeyboardAvoidingTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KeyboardAvoiding 2 | 3 | A SwiftUI view that manages a UIViewController that responds to keyboard events with modified additionalSafeAreaInsets. 4 | 5 | ## Why KeyboardAvoiding? 6 | 7 | | Without KeyboardAvoiding | With KeyboardAvoiding | 8 | | ------------------------ | --------------------- | 9 | | ![Screenshot](.github/without-keyboardavoiding.png) | ![Screenshot](.github/with-keyboardavoiding.png) | 10 | -------------------------------------------------------------------------------- /Tests/KeyboardAvoidingTests/KeyboardAvoidingTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import KeyboardAvoiding 3 | 4 | final class KeyboardAvoidingTests: XCTestCase { 5 | func testExample() { 6 | // This is an example of a functional test case. 7 | // Use XCTAssert and related functions to verify your tests produce the correct 8 | // results. 9 | } 10 | 11 | static var allTests = [ 12 | ("testExample", testExample), 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alexsander Akers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "KeyboardAvoiding", 8 | platforms: [ 9 | .iOS(.v13) 10 | ], 11 | products: [ 12 | // Products define the executables and libraries produced by a package, and make them visible to other packages. 13 | .library( 14 | name: "KeyboardAvoiding", 15 | targets: ["KeyboardAvoiding"]), 16 | ], 17 | dependencies: [ 18 | // Dependencies declare other packages that this package depends on. 19 | // .package(url: /* package url */, from: "1.0.0"), 20 | ], 21 | targets: [ 22 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 23 | // Targets can depend on other targets in this package, and on products in packages which this package depends on. 24 | .target( 25 | name: "KeyboardAvoiding", 26 | dependencies: []), 27 | .testTarget( 28 | name: "KeyboardAvoidingTests", 29 | dependencies: ["KeyboardAvoiding"]), 30 | ] 31 | ) 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | # Package.resolved 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | 51 | # Carthage 52 | # 53 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 54 | # Carthage/Checkouts 55 | 56 | Carthage/Build 57 | 58 | # fastlane 59 | # 60 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 61 | # screenshots whenever they are needed. 62 | # For more information about the recommended setup visit: 63 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 64 | 65 | fastlane/report.xml 66 | fastlane/Preview.html 67 | fastlane/screenshots/**/*.png 68 | fastlane/test_output 69 | -------------------------------------------------------------------------------- /Sources/KeyboardAvoiding/KeyboardAvoiding.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | class KeyboardAvoidingHostingController: UIHostingController where Content: View { 4 | override func viewDidAppear(_ animated: Bool) { 5 | super.viewDidAppear(animated) 6 | NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: UIResponder.keyboardWillChangeFrameNotification, object: nil) 7 | } 8 | 9 | override func viewDidDisappear(_ animated: Bool) { 10 | super.viewDidDisappear(animated) 11 | NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil) 12 | } 13 | 14 | @objc private func keyboardWillChangeFrame(_ notification: Notification) { 15 | guard isViewLoaded, let window = view.window, let userInfo = notification.userInfo else { 16 | return 17 | } 18 | 19 | guard let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else { 20 | return 21 | } 22 | 23 | guard let rawAnimationCurve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else { 24 | return 25 | } 26 | 27 | guard let endFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { 28 | return 29 | } 30 | 31 | let endFrameInWindow = window.convert(endFrame, from: nil) 32 | let endFrameInView = view.convert(endFrameInWindow, from: nil) 33 | let endFrameIntersection = view.bounds.intersection(endFrameInView) 34 | let keyboardHeight = view.bounds.maxY - endFrameIntersection.minY 35 | 36 | let options = UIView.AnimationOptions(rawValue: rawAnimationCurve << 16) 37 | UIView.animate(withDuration: duration, delay: 0, options: options, animations: { 38 | self.additionalSafeAreaInsets.bottom = keyboardHeight 39 | self.view.layoutIfNeeded() 40 | }) 41 | } 42 | } 43 | 44 | struct KeyboardAvoidingViewController: UIViewControllerRepresentable where Content: View { 45 | var rootView: Content 46 | 47 | func makeUIViewController(context: Context) -> KeyboardAvoidingHostingController { 48 | return KeyboardAvoidingHostingController(rootView: rootView) 49 | } 50 | 51 | func updateUIViewController(_ uiViewController: KeyboardAvoidingHostingController, context: Context) { 52 | uiViewController.rootView = rootView 53 | } 54 | } 55 | 56 | public struct KeyboardAvoidingView: View where Content: View { 57 | let content: Content 58 | 59 | public init(@ViewBuilder content: () -> Content) { 60 | self.content = content() 61 | } 62 | 63 | public var body: some View { 64 | return KeyboardAvoidingViewController(rootView: content) 65 | } 66 | } 67 | 68 | public struct KeyboardAvoiding: ViewModifier { 69 | public func body(content: _ViewModifier_Content) -> KeyboardAvoidingView<_ViewModifier_Content> { 70 | return KeyboardAvoidingView { content } 71 | } 72 | } 73 | 74 | public extension View { 75 | func keyboardAvoiding() -> Self.Modified { 76 | return modifier(KeyboardAvoiding()) 77 | } 78 | } 79 | --------------------------------------------------------------------------------