├── .github └── workflows │ └── deploy_to_cococapods.yml ├── .gitignore ├── KeyboardAvoider-Example ├── KeyboardAvoider-Example.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── KeyboardAvoider-Example │ ├── AppDelegate.swift │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ └── LaunchScreen.storyboard │ ├── ContentView.swift │ ├── Info.plist │ ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json │ ├── SceneDelegate.swift │ ├── UIResponder_Extension.swift │ └── Views │ ├── NormalTextField.swift │ └── PrimaryButton.swift ├── KeyboardAvoider.podspec ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── KeyboardAvoider │ ├── KeyboardAvoider.swift │ ├── KeyboardAvoiderModifier.swift │ ├── KeyboardHandler.swift │ └── UIResponder_Extension.swift ├── Tests └── KeyboardAvoiderTests │ ├── KeyboardAvoiderTests.swift │ └── XCTestManifests.swift └── images ├── screenshot.gif └── screenshot2.gif /.github/workflows/deploy_to_cococapods.yml: -------------------------------------------------------------------------------- 1 | name: deploy_to_cocoapods 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: macOS-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - uses: michaelhenry/deploy-to-cocoapods-github-action@1.0.9 16 | env: 17 | DEVELOPER_DIR: /Applications/Xcode_11.app 18 | COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | xcuserdata/ 5 | DerivedData 6 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A6A76BFC23D90F91008DD1A0 /* UIResponder_Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6A76BFB23D90F91008DD1A0 /* UIResponder_Extension.swift */; }; 11 | A6BD94C123C5E174000142A5 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94C023C5E174000142A5 /* AppDelegate.swift */; }; 12 | A6BD94C323C5E174000142A5 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94C223C5E174000142A5 /* SceneDelegate.swift */; }; 13 | A6BD94C523C5E174000142A5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94C423C5E174000142A5 /* ContentView.swift */; }; 14 | A6BD94C723C5E176000142A5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A6BD94C623C5E176000142A5 /* Assets.xcassets */; }; 15 | A6BD94CA23C5E176000142A5 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A6BD94C923C5E176000142A5 /* Preview Assets.xcassets */; }; 16 | A6BD94CD23C5E176000142A5 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A6BD94CB23C5E176000142A5 /* LaunchScreen.storyboard */; }; 17 | A6BD94E123C5E269000142A5 /* NormalTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94E023C5E269000142A5 /* NormalTextField.swift */; }; 18 | A6BD94E323C5E2A7000142A5 /* PrimaryButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94E223C5E2A7000142A5 /* PrimaryButton.swift */; }; 19 | A6BD94E823C5E574000142A5 /* KeyboardHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94E423C5E574000142A5 /* KeyboardHandler.swift */; }; 20 | A6BD94E923C5E574000142A5 /* KeyboardAvoider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94E523C5E574000142A5 /* KeyboardAvoider.swift */; }; 21 | A6BD94EB23C5E574000142A5 /* KeyboardAvoiderModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BD94E723C5E574000142A5 /* KeyboardAvoiderModifier.swift */; }; 22 | /* End PBXBuildFile section */ 23 | 24 | /* Begin PBXFileReference section */ 25 | A6A76BFB23D90F91008DD1A0 /* UIResponder_Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UIResponder_Extension.swift; path = ../../Sources/KeyboardAvoider/UIResponder_Extension.swift; sourceTree = ""; }; 26 | A6BD94BD23C5E174000142A5 /* KeyboardAvoider-Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "KeyboardAvoider-Example.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | A6BD94C023C5E174000142A5 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 28 | A6BD94C223C5E174000142A5 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 29 | A6BD94C423C5E174000142A5 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 30 | A6BD94C623C5E176000142A5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 31 | A6BD94C923C5E176000142A5 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 32 | A6BD94CC23C5E176000142A5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 33 | A6BD94CE23C5E176000142A5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34 | A6BD94E023C5E269000142A5 /* NormalTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NormalTextField.swift; sourceTree = ""; }; 35 | A6BD94E223C5E2A7000142A5 /* PrimaryButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimaryButton.swift; sourceTree = ""; }; 36 | A6BD94E423C5E574000142A5 /* KeyboardHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KeyboardHandler.swift; path = ../../Sources/KeyboardAvoider/KeyboardHandler.swift; sourceTree = ""; }; 37 | A6BD94E523C5E574000142A5 /* KeyboardAvoider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KeyboardAvoider.swift; path = ../../Sources/KeyboardAvoider/KeyboardAvoider.swift; sourceTree = ""; }; 38 | A6BD94E723C5E574000142A5 /* KeyboardAvoiderModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KeyboardAvoiderModifier.swift; path = ../../Sources/KeyboardAvoider/KeyboardAvoiderModifier.swift; sourceTree = ""; }; 39 | /* End PBXFileReference section */ 40 | 41 | /* Begin PBXFrameworksBuildPhase section */ 42 | A6BD94BA23C5E174000142A5 /* Frameworks */ = { 43 | isa = PBXFrameworksBuildPhase; 44 | buildActionMask = 2147483647; 45 | files = ( 46 | ); 47 | runOnlyForDeploymentPostprocessing = 0; 48 | }; 49 | /* End PBXFrameworksBuildPhase section */ 50 | 51 | /* Begin PBXGroup section */ 52 | A6BD94B423C5E174000142A5 = { 53 | isa = PBXGroup; 54 | children = ( 55 | A6BD94BF23C5E174000142A5 /* KeyboardAvoider-Example */, 56 | A6BD94BE23C5E174000142A5 /* Products */, 57 | ); 58 | sourceTree = ""; 59 | }; 60 | A6BD94BE23C5E174000142A5 /* Products */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | A6BD94BD23C5E174000142A5 /* KeyboardAvoider-Example.app */, 64 | ); 65 | name = Products; 66 | sourceTree = ""; 67 | }; 68 | A6BD94BF23C5E174000142A5 /* KeyboardAvoider-Example */ = { 69 | isa = PBXGroup; 70 | children = ( 71 | A6BD94DF23C5E258000142A5 /* Views */, 72 | A6BD94DE23C5E1C8000142A5 /* Sources */, 73 | A6BD94C023C5E174000142A5 /* AppDelegate.swift */, 74 | A6BD94C223C5E174000142A5 /* SceneDelegate.swift */, 75 | A6BD94C423C5E174000142A5 /* ContentView.swift */, 76 | A6BD94C623C5E176000142A5 /* Assets.xcassets */, 77 | A6BD94CB23C5E176000142A5 /* LaunchScreen.storyboard */, 78 | A6BD94CE23C5E176000142A5 /* Info.plist */, 79 | A6BD94C823C5E176000142A5 /* Preview Content */, 80 | ); 81 | path = "KeyboardAvoider-Example"; 82 | sourceTree = ""; 83 | }; 84 | A6BD94C823C5E176000142A5 /* Preview Content */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | A6BD94C923C5E176000142A5 /* Preview Assets.xcassets */, 88 | ); 89 | path = "Preview Content"; 90 | sourceTree = ""; 91 | }; 92 | A6BD94DE23C5E1C8000142A5 /* Sources */ = { 93 | isa = PBXGroup; 94 | children = ( 95 | A6A76BFB23D90F91008DD1A0 /* UIResponder_Extension.swift */, 96 | A6BD94E523C5E574000142A5 /* KeyboardAvoider.swift */, 97 | A6BD94E723C5E574000142A5 /* KeyboardAvoiderModifier.swift */, 98 | A6BD94E423C5E574000142A5 /* KeyboardHandler.swift */, 99 | ); 100 | name = Sources; 101 | path = ../Sources/KeyboardAvoider; 102 | sourceTree = SOURCE_ROOT; 103 | }; 104 | A6BD94DF23C5E258000142A5 /* Views */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | A6BD94E023C5E269000142A5 /* NormalTextField.swift */, 108 | A6BD94E223C5E2A7000142A5 /* PrimaryButton.swift */, 109 | ); 110 | path = Views; 111 | sourceTree = ""; 112 | }; 113 | /* End PBXGroup section */ 114 | 115 | /* Begin PBXNativeTarget section */ 116 | A6BD94BC23C5E174000142A5 /* KeyboardAvoider-Example */ = { 117 | isa = PBXNativeTarget; 118 | buildConfigurationList = A6BD94D123C5E176000142A5 /* Build configuration list for PBXNativeTarget "KeyboardAvoider-Example" */; 119 | buildPhases = ( 120 | A6BD94B923C5E174000142A5 /* Sources */, 121 | A6BD94BA23C5E174000142A5 /* Frameworks */, 122 | A6BD94BB23C5E174000142A5 /* Resources */, 123 | ); 124 | buildRules = ( 125 | ); 126 | dependencies = ( 127 | ); 128 | name = "KeyboardAvoider-Example"; 129 | productName = "KeyboardAvoider-Example"; 130 | productReference = A6BD94BD23C5E174000142A5 /* KeyboardAvoider-Example.app */; 131 | productType = "com.apple.product-type.application"; 132 | }; 133 | /* End PBXNativeTarget section */ 134 | 135 | /* Begin PBXProject section */ 136 | A6BD94B523C5E174000142A5 /* Project object */ = { 137 | isa = PBXProject; 138 | attributes = { 139 | LastSwiftUpdateCheck = 1130; 140 | LastUpgradeCheck = 1130; 141 | ORGANIZATIONNAME = "Michael Henry Pantaleon"; 142 | TargetAttributes = { 143 | A6BD94BC23C5E174000142A5 = { 144 | CreatedOnToolsVersion = 11.3; 145 | }; 146 | }; 147 | }; 148 | buildConfigurationList = A6BD94B823C5E174000142A5 /* Build configuration list for PBXProject "KeyboardAvoider-Example" */; 149 | compatibilityVersion = "Xcode 9.3"; 150 | developmentRegion = en; 151 | hasScannedForEncodings = 0; 152 | knownRegions = ( 153 | en, 154 | Base, 155 | ); 156 | mainGroup = A6BD94B423C5E174000142A5; 157 | productRefGroup = A6BD94BE23C5E174000142A5 /* Products */; 158 | projectDirPath = ""; 159 | projectRoot = ""; 160 | targets = ( 161 | A6BD94BC23C5E174000142A5 /* KeyboardAvoider-Example */, 162 | ); 163 | }; 164 | /* End PBXProject section */ 165 | 166 | /* Begin PBXResourcesBuildPhase section */ 167 | A6BD94BB23C5E174000142A5 /* Resources */ = { 168 | isa = PBXResourcesBuildPhase; 169 | buildActionMask = 2147483647; 170 | files = ( 171 | A6BD94CD23C5E176000142A5 /* LaunchScreen.storyboard in Resources */, 172 | A6BD94CA23C5E176000142A5 /* Preview Assets.xcassets in Resources */, 173 | A6BD94C723C5E176000142A5 /* Assets.xcassets in Resources */, 174 | ); 175 | runOnlyForDeploymentPostprocessing = 0; 176 | }; 177 | /* End PBXResourcesBuildPhase section */ 178 | 179 | /* Begin PBXSourcesBuildPhase section */ 180 | A6BD94B923C5E174000142A5 /* Sources */ = { 181 | isa = PBXSourcesBuildPhase; 182 | buildActionMask = 2147483647; 183 | files = ( 184 | A6BD94E323C5E2A7000142A5 /* PrimaryButton.swift in Sources */, 185 | A6BD94E123C5E269000142A5 /* NormalTextField.swift in Sources */, 186 | A6BD94C123C5E174000142A5 /* AppDelegate.swift in Sources */, 187 | A6BD94E823C5E574000142A5 /* KeyboardHandler.swift in Sources */, 188 | A6BD94C323C5E174000142A5 /* SceneDelegate.swift in Sources */, 189 | A6A76BFC23D90F91008DD1A0 /* UIResponder_Extension.swift in Sources */, 190 | A6BD94E923C5E574000142A5 /* KeyboardAvoider.swift in Sources */, 191 | A6BD94EB23C5E574000142A5 /* KeyboardAvoiderModifier.swift in Sources */, 192 | A6BD94C523C5E174000142A5 /* ContentView.swift in Sources */, 193 | ); 194 | runOnlyForDeploymentPostprocessing = 0; 195 | }; 196 | /* End PBXSourcesBuildPhase section */ 197 | 198 | /* Begin PBXVariantGroup section */ 199 | A6BD94CB23C5E176000142A5 /* LaunchScreen.storyboard */ = { 200 | isa = PBXVariantGroup; 201 | children = ( 202 | A6BD94CC23C5E176000142A5 /* Base */, 203 | ); 204 | name = LaunchScreen.storyboard; 205 | sourceTree = ""; 206 | }; 207 | /* End PBXVariantGroup section */ 208 | 209 | /* Begin XCBuildConfiguration section */ 210 | A6BD94CF23C5E176000142A5 /* Debug */ = { 211 | isa = XCBuildConfiguration; 212 | buildSettings = { 213 | ALWAYS_SEARCH_USER_PATHS = NO; 214 | CLANG_ANALYZER_NONNULL = YES; 215 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 216 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 217 | CLANG_CXX_LIBRARY = "libc++"; 218 | CLANG_ENABLE_MODULES = YES; 219 | CLANG_ENABLE_OBJC_ARC = YES; 220 | CLANG_ENABLE_OBJC_WEAK = YES; 221 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 222 | CLANG_WARN_BOOL_CONVERSION = YES; 223 | CLANG_WARN_COMMA = YES; 224 | CLANG_WARN_CONSTANT_CONVERSION = YES; 225 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 226 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 227 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 228 | CLANG_WARN_EMPTY_BODY = YES; 229 | CLANG_WARN_ENUM_CONVERSION = YES; 230 | CLANG_WARN_INFINITE_RECURSION = YES; 231 | CLANG_WARN_INT_CONVERSION = YES; 232 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 233 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 234 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 235 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 236 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 237 | CLANG_WARN_STRICT_PROTOTYPES = YES; 238 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 239 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 240 | CLANG_WARN_UNREACHABLE_CODE = YES; 241 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 242 | COPY_PHASE_STRIP = NO; 243 | DEBUG_INFORMATION_FORMAT = dwarf; 244 | ENABLE_STRICT_OBJC_MSGSEND = YES; 245 | ENABLE_TESTABILITY = YES; 246 | GCC_C_LANGUAGE_STANDARD = gnu11; 247 | GCC_DYNAMIC_NO_PIC = NO; 248 | GCC_NO_COMMON_BLOCKS = YES; 249 | GCC_OPTIMIZATION_LEVEL = 0; 250 | GCC_PREPROCESSOR_DEFINITIONS = ( 251 | "DEBUG=1", 252 | "$(inherited)", 253 | ); 254 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 255 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 256 | GCC_WARN_UNDECLARED_SELECTOR = YES; 257 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 258 | GCC_WARN_UNUSED_FUNCTION = YES; 259 | GCC_WARN_UNUSED_VARIABLE = YES; 260 | IPHONEOS_DEPLOYMENT_TARGET = 13.2; 261 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 262 | MTL_FAST_MATH = YES; 263 | ONLY_ACTIVE_ARCH = YES; 264 | SDKROOT = iphoneos; 265 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 266 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 267 | }; 268 | name = Debug; 269 | }; 270 | A6BD94D023C5E176000142A5 /* Release */ = { 271 | isa = XCBuildConfiguration; 272 | buildSettings = { 273 | ALWAYS_SEARCH_USER_PATHS = NO; 274 | CLANG_ANALYZER_NONNULL = YES; 275 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 276 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 277 | CLANG_CXX_LIBRARY = "libc++"; 278 | CLANG_ENABLE_MODULES = YES; 279 | CLANG_ENABLE_OBJC_ARC = YES; 280 | CLANG_ENABLE_OBJC_WEAK = YES; 281 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 282 | CLANG_WARN_BOOL_CONVERSION = YES; 283 | CLANG_WARN_COMMA = YES; 284 | CLANG_WARN_CONSTANT_CONVERSION = YES; 285 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 286 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 287 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 288 | CLANG_WARN_EMPTY_BODY = YES; 289 | CLANG_WARN_ENUM_CONVERSION = YES; 290 | CLANG_WARN_INFINITE_RECURSION = YES; 291 | CLANG_WARN_INT_CONVERSION = YES; 292 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 293 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 294 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 295 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 296 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 297 | CLANG_WARN_STRICT_PROTOTYPES = YES; 298 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 299 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 300 | CLANG_WARN_UNREACHABLE_CODE = YES; 301 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 302 | COPY_PHASE_STRIP = NO; 303 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 304 | ENABLE_NS_ASSERTIONS = NO; 305 | ENABLE_STRICT_OBJC_MSGSEND = YES; 306 | GCC_C_LANGUAGE_STANDARD = gnu11; 307 | GCC_NO_COMMON_BLOCKS = YES; 308 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 309 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 310 | GCC_WARN_UNDECLARED_SELECTOR = YES; 311 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 312 | GCC_WARN_UNUSED_FUNCTION = YES; 313 | GCC_WARN_UNUSED_VARIABLE = YES; 314 | IPHONEOS_DEPLOYMENT_TARGET = 13.2; 315 | MTL_ENABLE_DEBUG_INFO = NO; 316 | MTL_FAST_MATH = YES; 317 | SDKROOT = iphoneos; 318 | SWIFT_COMPILATION_MODE = wholemodule; 319 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 320 | VALIDATE_PRODUCT = YES; 321 | }; 322 | name = Release; 323 | }; 324 | A6BD94D223C5E176000142A5 /* Debug */ = { 325 | isa = XCBuildConfiguration; 326 | buildSettings = { 327 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 328 | CODE_SIGN_STYLE = Automatic; 329 | DEVELOPMENT_ASSET_PATHS = "\"KeyboardAvoider-Example/Preview Content\""; 330 | ENABLE_PREVIEWS = YES; 331 | INFOPLIST_FILE = "KeyboardAvoider-Example/Info.plist"; 332 | LD_RUNPATH_SEARCH_PATHS = ( 333 | "$(inherited)", 334 | "@executable_path/Frameworks", 335 | ); 336 | PRODUCT_BUNDLE_IDENTIFIER = "net.iamkel.KeyboardAvoider-Example"; 337 | PRODUCT_NAME = "$(TARGET_NAME)"; 338 | SWIFT_VERSION = 5.0; 339 | TARGETED_DEVICE_FAMILY = "1,2"; 340 | }; 341 | name = Debug; 342 | }; 343 | A6BD94D323C5E176000142A5 /* Release */ = { 344 | isa = XCBuildConfiguration; 345 | buildSettings = { 346 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 347 | CODE_SIGN_STYLE = Automatic; 348 | DEVELOPMENT_ASSET_PATHS = "\"KeyboardAvoider-Example/Preview Content\""; 349 | ENABLE_PREVIEWS = YES; 350 | INFOPLIST_FILE = "KeyboardAvoider-Example/Info.plist"; 351 | LD_RUNPATH_SEARCH_PATHS = ( 352 | "$(inherited)", 353 | "@executable_path/Frameworks", 354 | ); 355 | PRODUCT_BUNDLE_IDENTIFIER = "net.iamkel.KeyboardAvoider-Example"; 356 | PRODUCT_NAME = "$(TARGET_NAME)"; 357 | SWIFT_VERSION = 5.0; 358 | TARGETED_DEVICE_FAMILY = "1,2"; 359 | }; 360 | name = Release; 361 | }; 362 | /* End XCBuildConfiguration section */ 363 | 364 | /* Begin XCConfigurationList section */ 365 | A6BD94B823C5E174000142A5 /* Build configuration list for PBXProject "KeyboardAvoider-Example" */ = { 366 | isa = XCConfigurationList; 367 | buildConfigurations = ( 368 | A6BD94CF23C5E176000142A5 /* Debug */, 369 | A6BD94D023C5E176000142A5 /* Release */, 370 | ); 371 | defaultConfigurationIsVisible = 0; 372 | defaultConfigurationName = Release; 373 | }; 374 | A6BD94D123C5E176000142A5 /* Build configuration list for PBXNativeTarget "KeyboardAvoider-Example" */ = { 375 | isa = XCConfigurationList; 376 | buildConfigurations = ( 377 | A6BD94D223C5E176000142A5 /* Debug */, 378 | A6BD94D323C5E176000142A5 /* Release */, 379 | ); 380 | defaultConfigurationIsVisible = 0; 381 | defaultConfigurationName = Release; 382 | }; 383 | /* End XCConfigurationList section */ 384 | }; 385 | rootObject = A6BD94B523C5E174000142A5 /* Project object */; 386 | } 387 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | // MARK: UISceneSession Lifecycle 22 | 23 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 24 | // Called when a new scene session is being created. 25 | // Use this method to select a configuration to create the new scene with. 26 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 27 | } 28 | 29 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 30 | // Called when the user discards a scene session. 31 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 32 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct ContentView: View { 12 | 13 | @State var text:String = "" 14 | 15 | var body: some View { 16 | KeyboardAvoider { 17 | ZStack { 18 | VStack(spacing: 16.0) { 19 | Text("Sign up") 20 | .font(.largeTitle) 21 | .multilineTextAlignment(.leading) 22 | 23 | Group { 24 | NormalTextField("First name", text: self.$text) 25 | NormalTextField("Last name", text: self.$text) 26 | NormalTextField("Email", text: self.$text) 27 | NormalTextField ("Phone number", text: self.$text) 28 | NormalTextField("Password", text: self.$text) 29 | NormalTextField ("Confirm password", text: self.$text) 30 | NormalTextField ("Confirm password", text: self.$text) 31 | NormalTextField ("Confirm password", text: self.$text) 32 | NormalTextField ("Confirm password", text: self.$text) 33 | NormalTextField ("Confirm password", text: self.$text) 34 | } 35 | 36 | Group { 37 | PrimaryButton("Sign up") { 38 | 39 | } 40 | 41 | Button("Already have an account?") { 42 | 43 | } 44 | } 45 | } 46 | .padding(.horizontal, 16.0) 47 | } 48 | } 49 | .edgesIgnoringSafeArea(.bottom) 50 | } 51 | } 52 | 53 | struct ContentView_Previews: PreviewProvider { 54 | static var previews: some View { 55 | ContentView() 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SwiftUI 11 | 12 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | 22 | // Create the SwiftUI view that provides the window contents. 23 | let contentView = ContentView() 24 | 25 | // Use a UIHostingController as window root view controller. 26 | if let windowScene = scene as? UIWindowScene { 27 | let window = UIWindow(windowScene: windowScene) 28 | window.rootViewController = UIHostingController(rootView: contentView) 29 | self.window = window 30 | window.makeKeyAndVisible() 31 | } 32 | } 33 | 34 | func sceneDidDisconnect(_ scene: UIScene) { 35 | // Called as the scene is being released by the system. 36 | // This occurs shortly after the scene enters the background, or when its session is discarded. 37 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 38 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 39 | } 40 | 41 | func sceneDidBecomeActive(_ scene: UIScene) { 42 | // Called when the scene has moved from an inactive state to an active state. 43 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 44 | } 45 | 46 | func sceneWillResignActive(_ scene: UIScene) { 47 | // Called when the scene will move from an active state to an inactive state. 48 | // This may occur due to temporary interruptions (ex. an incoming phone call). 49 | } 50 | 51 | func sceneWillEnterForeground(_ scene: UIScene) { 52 | // Called as the scene transitions from the background to the foreground. 53 | // Use this method to undo the changes made on entering the background. 54 | } 55 | 56 | func sceneDidEnterBackground(_ scene: UIScene) { 57 | // Called as the scene transitions from the foreground to the background. 58 | // Use this method to save data, release shared resources, and store enough scene-specific state information 59 | // to restore the scene back to its current state. 60 | } 61 | 62 | 63 | } 64 | 65 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/UIResponder_Extension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIResponder_Extension.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/22. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | extension UIResponder { 12 | 13 | private struct Static { 14 | static weak var responder: UIResponder? 15 | } 16 | 17 | static func current() -> UIResponder? { 18 | Static.responder = nil 19 | UIApplication.shared.sendAction( 20 | #selector(UIResponder.trap), 21 | to: nil, from: nil, for: nil) 22 | return Static.responder 23 | } 24 | 25 | @objc private func trap() { 26 | Static.responder = self 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Views/NormalTextField.swift: -------------------------------------------------------------------------------- 1 | // 2 | // NormalTextField.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct NormalTextField: View { 12 | 13 | var placeholder:String = "" 14 | var text:Binding 15 | 16 | 17 | init(_ placeholder:String, text: Binding) { 18 | self.placeholder = placeholder 19 | self.text = text 20 | } 21 | 22 | var body: some View { 23 | 24 | TextField(self.placeholder, text: self.text) 25 | .padding() 26 | .frame( 27 | minWidth: 0, 28 | idealWidth: nil, 29 | maxWidth: .infinity, 30 | minHeight: 44, 31 | idealHeight: 44, 32 | maxHeight: 44, 33 | alignment: .center) 34 | .foregroundColor(Color(UIColor.black)) 35 | .overlay( 36 | RoundedRectangle(cornerRadius: 8) 37 | .stroke(Color(UIColor.black), lineWidth: 1) 38 | ) 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /KeyboardAvoider-Example/KeyboardAvoider-Example/Views/PrimaryButton.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PrimaryButton.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct PrimaryButton: View { 12 | 13 | var title:String = "" 14 | var action:(() -> Void) 15 | 16 | init(_ title:String, action: @escaping (() -> Void)) { 17 | self.title = title 18 | self.action = action 19 | } 20 | 21 | var body: some View { 22 | 23 | Button(action: { 24 | self.action() 25 | }) { 26 | Text(self.title) 27 | .fontWeight(.medium) 28 | .lineLimit(0) 29 | .padding() 30 | .frame( 31 | minWidth: 0, 32 | idealWidth: nil, 33 | maxWidth: 200, 34 | minHeight: 44, 35 | idealHeight: 44, 36 | maxHeight: 44, 37 | alignment: .center) 38 | .foregroundColor(Color(UIColor.darkGray)) 39 | .overlay( 40 | RoundedRectangle(cornerRadius: 22) 41 | .stroke(Color(UIColor.darkGray), lineWidth: 1) 42 | ) 43 | } 44 | 45 | } 46 | 47 | } 48 | 49 | struct PrimaryButton_Previews: PreviewProvider { 50 | static var previews: some View { 51 | PrimaryButton("Title here") { 52 | 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /KeyboardAvoider.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'KeyboardAvoider' 3 | s.version = ENV['LIB_VERSION'] || '1.0' 4 | s.summary = 'The missing interactive keyboard in SwiftUI for iOS' 5 | s.description = <<-DESC 6 | The missing interactive keyboard in SwiftUI for iOS. 7 | DESC 8 | 9 | s.homepage = 'https://github.com/michaelhenry/KeyboardAvoider' 10 | s.license = { :type => 'MIT', :file => 'LICENSE' } 11 | s.author = { 'michaelhenry' => 'me@iamkel.net' } 12 | s.source = { :git => 'https://github.com/michaelhenry/KeyboardAvoider.git', :tag => s.version.to_s } 13 | s.social_media_url = 'https://twitter.com/michaelhenry119' 14 | s.ios.deployment_target = '13.0' 15 | s.source_files = 'Sources/KeyboardAvoider/**/*' 16 | s.swift_versions = ['5.0', '5.1'] 17 | s.frameworks = 'UIKit','Combine','Foundation', 'SwiftUI' 18 | end -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Michael Henry Pantaleon 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: "KeyboardAvoider", 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: "KeyboardAvoider", 15 | targets: ["KeyboardAvoider"]), 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: "KeyboardAvoider", 26 | dependencies: []), 27 | .testTarget( 28 | name: "KeyboardAvoiderTests", 29 | dependencies: ["KeyboardAvoider"]), 30 | ] 31 | ) 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Screenshot](images/screenshot.gif) ![Screenshot](images/screenshot2.gif) 2 | 3 | [![Deployment status](https://github.com/michaelhenry/KeyboardAvoider/workflows/deploy_to_cocoapods/badge.svg)](https://github.com/michaelhenry/KeyboardAvoider/actions) 4 | [![Version](https://img.shields.io/cocoapods/v/KeyboardAvoider.svg?style=flat)](https://cocoapods.org/pods/KeyboardAvoider) 5 | [![License](https://img.shields.io/cocoapods/l/KeyboardAvoider.svg?style=flat)](https://cocoapods.org/pods/KeyboardAvoider) 6 | [![Platform](https://img.shields.io/cocoapods/p/KeyboardAvoider.svg?style=flat)](https://cocoapods.org/pods/KeyboardAvoider) 7 | 8 | # ⌨️ KeyboardAvoider {} 9 | 10 | A **KeyboardAvoider** for SwiftUI. Inspired by the simplicity of [keyboard_avoider](https://pub.dev/packages/keyboard_avoider) in [Flutter](https://flutter.dev/). 11 | 12 | ## Features 13 | - Autoscroll to TextField 14 | - Swipe keyboard to dismiss 15 | 16 | 17 | ## Installation 18 | 19 | ### [Swift Package Manager](https://github.com/apple/swift-package-manager) 20 | 21 | Create a `Package.swift` file. 22 | 23 | ```swift 24 | import PackageDescription 25 | 26 | let package = Package( 27 | name: "TestProject", 28 | dependencies: [ 29 | .package(url: "https://github.com/michaelhenry/KeyboardAvoider.git", from: "1.0.0") 30 | ] 31 | ) 32 | ``` 33 | 34 | ### Cocoapods 35 | 36 | ```ruby 37 | target 'MyApp' do 38 | pod 'KeyboardAvoider', '~> 1.0' 39 | end 40 | ``` 41 | 42 | ## How to use 43 | 44 | ```swift 45 | import KeyboardAvoider 46 | 47 | KeyboardAvoider { 48 | // ... Your view with TextFields 49 | } 50 | ``` 51 | 52 | Example: 53 | 54 | ```swift 55 | KeyboardAvoider { 56 | VStack { 57 | TextField("First name", text: self.$firstname) 58 | TextField("Last name", text: self.$lastname) 59 | TextField("Email", text: self.$email) 60 | TextField("Password", text: self.$password) 61 | TextField("Confirm password", text: self.$password) 62 | Button("Sign Up") { 63 | 64 | } 65 | Button("Already have an account?") { 66 | 67 | } 68 | } 69 | .padding(.horizontal, 16.0) 70 | } 71 | ``` 72 | 73 | Or in case you don't want to make your view scrollable, you can just only apply the `.avoidKeyboard()` into your main view. 74 | 75 | ```swift 76 | VStack { 77 | TextField("First name", text: self.$firstname) 78 | TextField("Last name", text: self.$lastname) 79 | TextField("Email", text: self.$email) 80 | TextField("Password", text: self.$password) 81 | TextField("Confirm password", text: self.$password) 82 | Button("Sign Up") { 83 | 84 | } 85 | Button("Already have an account?") { 86 | 87 | } 88 | } 89 | .avoidKeyboard() 90 | ``` 91 | 92 | ## FAQ 93 | - How to remove the extra space between the textfield and the keyboard 94 | 95 | > You can remove it by ignoring the `safe area - bottom`. Please see the [Sample Project](https://github.com/michaelhenry/KeyboardAvoider/blob/d5293c541673bce47f00cdd0ec2f1b604b5341c8/KeyboardAvoider-Example/KeyboardAvoider-Example/ContentView.swift#L45) 96 | 97 | ```swift 98 | .edgesIgnoringSafeArea(.bottom) 99 | ``` 100 | 101 | -------------------------------------------------------------------------------- /Sources/KeyboardAvoider/KeyboardAvoider.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KeyboardAvoider.swift 3 | // KeyboardAvoider 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // 7 | 8 | #if os(iOS) 9 | 10 | import SwiftUI 11 | 12 | public struct KeyboardAvoider: View { 13 | 14 | private(set) var content:Content 15 | 16 | public init(@ViewBuilder _ content: () -> Content) { 17 | self.content = content() 18 | } 19 | 20 | public var body: some View { 21 | 22 | ScrollView { 23 | self.content 24 | } 25 | .avoidKeyboard() 26 | } 27 | } 28 | 29 | #endif 30 | 31 | 32 | -------------------------------------------------------------------------------- /Sources/KeyboardAvoider/KeyboardAvoiderModifier.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KeyboardAvoiderModifier.swift 3 | // KeyboardAvoider 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // 7 | 8 | #if os(iOS) 9 | 10 | import SwiftUI 11 | import Combine 12 | 13 | public struct KeyboardAvoiderModifier: ViewModifier { 14 | 15 | @ObservedObject var keyboardHandler = KeyboardHandler() 16 | 17 | public func body(content: Content) -> some View { 18 | content 19 | .padding(.bottom, keyboardHandler.keyboardHeight) 20 | } 21 | } 22 | 23 | extension View { 24 | public func avoidKeyboard() -> some View { 25 | modifier(KeyboardAvoiderModifier()) 26 | } 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Sources/KeyboardAvoider/KeyboardHandler.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KeyboardHandler.swift 3 | // KeyboardAvoider 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/08. 6 | // 7 | 8 | #if os(iOS) 9 | 10 | import Foundation 11 | import Combine 12 | import UIKit 13 | 14 | class KeyboardHandler:NSObject, ObservableObject, UIGestureRecognizerDelegate { 15 | 16 | @Published public var keyboardHeight:CGFloat = 0.0 17 | 18 | /// Space between keyboard and TextField or TextView 19 | var spaceBetweenKeyboardAndInputField:CGFloat = 20.0 20 | 21 | var actualKeyboardHeight:CGFloat? 22 | 23 | var panRecognizer:UIPanGestureRecognizer? 24 | var subscriptions = Set() 25 | 26 | override init() { 27 | super.init() 28 | panRecognizer = UIPanGestureRecognizer( 29 | target: self, action: #selector(handlePan(_:))) 30 | panRecognizer?.delegate = self 31 | UIApplication.shared.windows.first? 32 | .addGestureRecognizer(panRecognizer!) 33 | 34 | let keyboardWillShow = NotificationCenter 35 | .default 36 | .publisher(for: UIResponder.keyboardWillShowNotification) 37 | .compactMap({ $0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] 38 | as? CGRect }) 39 | .map { $0.height } 40 | .eraseToAnyPublisher() 41 | 42 | let keyboardWillHide = NotificationCenter 43 | .default 44 | .publisher(for: UIResponder.keyboardWillHideNotification) 45 | .map({ _ in CGFloat(0.0) }) 46 | .eraseToAnyPublisher() 47 | 48 | keyboardWillShow.merge(with: keyboardWillHide) 49 | .sink {[weak self] height in 50 | self?.keyboardHeight = height 51 | self?.actualKeyboardHeight = height 52 | } 53 | .store(in: &subscriptions) 54 | 55 | let keyboardDidShow = NotificationCenter 56 | .default 57 | .publisher(for: UIResponder.keyboardDidShowNotification) 58 | 59 | keyboardDidShow 60 | .sink {[weak self] _ in 61 | self?.adjustScrollViewOffsetYIfPossible() 62 | } 63 | .store(in: &subscriptions) 64 | } 65 | 66 | @objc func handlePan( 67 | _ gestureRecognizer:UIPanGestureRecognizer) { 68 | guard case .changed = gestureRecognizer.state, 69 | let window = UIApplication.shared.windows.first, 70 | let actualKbHeight = actualKeyboardHeight 71 | else { 72 | return 73 | } 74 | let originY = gestureRecognizer.location(in: window).y 75 | let screenHeight = UIScreen.main.bounds.height 76 | guard originY >= screenHeight - actualKbHeight else { 77 | return 78 | } 79 | keyboardHeight = screenHeight - originY 80 | } 81 | 82 | public func gestureRecognizer( 83 | _ gestureRecognizer: UIGestureRecognizer, 84 | shouldReceive touch: UITouch 85 | ) -> Bool { 86 | let point = touch.location(in: gestureRecognizer.view) 87 | var view = gestureRecognizer.view?.hitTest(point, with: nil) 88 | while let candidate = view { 89 | if let scrollView = candidate as? UIScrollView { 90 | scrollView.keyboardDismissMode = .interactive 91 | return true 92 | } 93 | view = candidate.superview 94 | } 95 | return false 96 | } 97 | 98 | public func gestureRecognizer( 99 | _ gestureRecognizer: UIGestureRecognizer, 100 | shouldRecognizeSimultaneouslyWith 101 | otherGestureRecognizer: UIGestureRecognizer 102 | ) -> Bool { 103 | return gestureRecognizer === self.panRecognizer 104 | } 105 | 106 | private func adjustScrollViewOffsetYIfPossible() { 107 | 108 | var activeView:UIView? 109 | if let activeTextField = UIResponder.current() as? UITextField { 110 | activeView = activeTextField 111 | } else if let activeTextView = UIResponder.current() as? UITextView { 112 | activeView = activeTextView 113 | } 114 | 115 | guard let _activeView = activeView else { return } 116 | 117 | // lookup for the parent scroll view 118 | var superview = _activeView.superview 119 | var scrollview:UIScrollView? 120 | while superview != nil { 121 | if let _sv = superview as? UIScrollView { 122 | scrollview = _sv 123 | break 124 | } 125 | superview = superview?.superview 126 | } 127 | 128 | guard let _scrollview = scrollview else { return } 129 | 130 | let targetFrame = _activeView.convert(_activeView.bounds, to: nil) 131 | let targetY = targetFrame.maxY 132 | let containerY = UIScreen.main.bounds.height - keyboardHeight 133 | 134 | if containerY < targetY { 135 | DispatchQueue.main.async { 136 | var newFrame = targetFrame 137 | newFrame.origin.y -= self.spaceBetweenKeyboardAndInputField 138 | _scrollview.scrollRectToVisible(newFrame, animated: true) 139 | } 140 | } 141 | } 142 | } 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /Sources/KeyboardAvoider/UIResponder_Extension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIResponder_Extension.swift 3 | // KeyboardAvoider-Example 4 | // 5 | // Created by Michael Henry Pantaleon on 2020/01/22. 6 | // Copyright © 2020 Michael Henry Pantaleon. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | extension UIResponder { 12 | 13 | private struct Static { 14 | static weak var responder: UIResponder? 15 | } 16 | 17 | static func current() -> UIResponder? { 18 | Static.responder = nil 19 | UIApplication.shared.sendAction( 20 | #selector(UIResponder.trap), 21 | to: nil, from: nil, for: nil) 22 | return Static.responder 23 | } 24 | 25 | @objc private func trap() { 26 | Static.responder = self 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Tests/KeyboardAvoiderTests/KeyboardAvoiderTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import KeyboardAvoider 3 | 4 | final class KeyboardAvoiderTests: 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 | XCTAssertEqual(KeyboardAvoider().text, "Hello, World!") 10 | } 11 | 12 | static var allTests = [ 13 | ("testExample", testExample), 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /Tests/KeyboardAvoiderTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(KeyboardAvoiderTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /images/screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelhenry/KeyboardAvoider/e1c3860e7b50bce567f3480ed443f1c6b3659725/images/screenshot.gif -------------------------------------------------------------------------------- /images/screenshot2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelhenry/KeyboardAvoider/e1c3860e7b50bce567f3480ed443f1c6b3659725/images/screenshot2.gif --------------------------------------------------------------------------------