IQKeyboardManager

repository·master·Indexed 12 days ago

https://github.com/hackiftekhar/iqkeyboardmanager

An iOS utility that automatically manages keyboard avoidance to ensure that the keyboard does not cover UITextField or UITextView. It features a modular architecture with subspecs for appearance, return key handling, toolbar management, and tap-to-resign functionality. Available for Swift and Objective-C via CocoaPods, Carthage, and Swift Package Manager.

Tokens
7.5K
Snippets
25
Records
34
Agent score
95%

What's inside IQKeyboardManager

  1. Important warnings for library developers

    master

    Do not include IQKeyboardManager in SDKs/Libraries

    Do not add IQKeyboardManager as a dependency when building a third-party SDK, library, or framework. It is designed for application-level convenience, not for being shipped within other libraries. Doing so can cause conflicts and block adoption by other developers.

    Handling conflicts

    If IQKeyboardManager conflicts with another third-party library in your project, it is the developer's responsibility to manually enable or disable IQKeyboardManager when presenting or dismissing the third-party library's UI.

  2. Migrate to IQKeyboardManagerWrapper for UIScrollView and UIView

    master

    Starting with version 7.0, direct extension properties on UIScrollView and UIView have been moved into a wrapper accessed via the .iq property. This provides a unified namespace for all IQKeyboardManager-related adjustments on these types.

    UIScrollView

    Access via scrollView.iq:

    • iq.ignoreScrollingAdjustment
    • iq.ignoreContentInsetAdjustment
    • iq.restoreContentOffset

    UIView

    Access via view.iq:

    • iq.distanceFromKeyboard
    • iq.ignoreSwitchingByNextPrevious
    • iq.enableMode
    • iq.resignOnTouchOutsideMode
    • iq.toolbar (to access the IQToolbar)
    • iq.hidePlaceholder
    • iq.placeholder
    • iq.drawingPlaceholder
    • iq.addToolbar(...) (to add a custom toolbar)
    • iq.viewContainingController()
    • iq.topMostController()
    • iq.parentContainerViewController()
    • iq.superviewOf(type:belowView:)
    // Example of using the new wrapper
    myScrollView.iq.ignoreScrollingAdjustment = true
    
    myTextField.iq.enableMode = .always
    myTextField.iq.distanceFromKeyboard = 20.0
  3. Migrate custom toolbar implementation to IQKeyboardToolbar

    master

    If you are building custom toolbars, many components have moved to the IQKeyboardToolbar library.

    Renamed Classes

    • IQToolbar $\rightarrow$ IQKeyboardToolbar
    • IQToolbarConfiguration $\rightarrow$ IQKeyboardToolbarConfiguration
    • IQToolbarPlaceholderConfiguration $\rightarrow$ IQKeyboardToolbarPlaceholderConfiguration
    • IQBarButtonItemConfiguration (remains the same)
    • IQTitleBarButtonItem (remains the same)
    • IQBarButtonItem (remains the same)
    • IQInvocation (remains the same)
    • IQPlaceholderable (protocol, remains the same)

    UITextField/UITextView Extensions

    Use these extensions to manage toolbars on specific text fields:

    • toolbar: Returns the IQKeyboardToolbar instance.
    • hidePlaceholder: (Bool) Hides the placeholder.
    • placeholder: (String?) The placeholder text.
    • drawingPlaceholder: (String?) The text used for drawing the placeholder.

    Toolbar Methods

    addToolbar(target:previousConfiguration:nextConfiguration:rightConfiguration:title:titleAccessibilityLabel:) addDone(...) addRightButton(...) addRightLeft(...) addPreviousNextRight(...) addPreviousNextDone(...)

    // Example of using extensions on a text field
    textField.iq.hidePlaceholder = true
    textField.iq.placeholder = "Enter text..."
    
    textField.iq.addDone(title: "Done")
  4. Install IQKeyboardManagerSwift via CocoaPods

    master

    To install the library using CocoaPods, add the following line to your Podfile:

    pod 'IQKeyboardManagerSwift'

    You can also specify a specific version if needed for compatibility with your Swift version:

    pod 'IQKeyboardManagerSwift', '8.0.0'
  5. Basic Setup: Enable automatic keyboard avoidance

    master

    To enable the core functionality (preventing the keyboard from covering UITextField or UITextView), import the library and set isEnabled to true in your AppDelegate.swift.

    import IQKeyboardManagerSwift
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, 
                         didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            // Enable keyboard management
            IQKeyboardManager.shared.isEnabled = true
    
            return true
        }
    }
  6. Migrate toolbar configuration from IQKeyboardManager to IQKeyboardToolbarManager

    master

    In version 8.0, several properties and classes related to toolbar management have been moved from IQKeyboardManager to IQKeyboardToolbarManager.

    Renamed Classes

    • IQPreviousNextView is now IQDeepResponderContainerView. If using this in a Storyboard, update the class name and change the module to IQKeyboardToolbarManager.
    • IQToolbarConfiguration is now IQKeyboardToolbarConfiguration.
    • IQToolbarPlaceholderConfiguration is now IQKeyboardToolbarPlaceholderConfiguration.

    Renamed Properties and Methods

    IQKeyboardManager.shared properties and methods have changed as follows:

    • enableAutoToolbar (Bool) $\rightarrow$ isEnabled (Bool)
    • toolbarConfiguration $\rightarrow$ toolbarConfiguration (using IQKeyboardToolbarConfiguration)
    • playInputClicks (Bool)
    • disabledToolbarClasses ([UIViewController.Type])
    • enabledToolbarClasses ([UIViewController.Type])
    • deepResponderAllowedContainerClasses ([UIView.Type])
    • canGoPrevious (Bool)
    • canGoNext (Bool)
    • goPrevious() $\rightarrow$ goPrevious() -> Bool
    • goNext() $\rightarrow$ goNext() -> Bool
    • reloadInputViews()

    UITextField/UITextView Extensions

    • ignoreSwitchingByNextPrevious (Bool) can be used via textField.iq.ignoreSwitchingByNextPrevious = true.
    // Example of using the new extension
    textField.iq.ignoreSwitchingByNextPrevious = true
  7. Migrate IQKeyboardManager shared instance access (v5.0 to v6.0)

    master

    When migrating from version 5.0 to 6.0, the methods used to access the shared IQKeyboardManager instance have changed. Replace the old sharedManager() or shared properties with the new implementation provided in version 6.0.

    // Old (v5.0)
    open class func sharedManager() -> IQKeyboardManager
    @objc public class var shared: IQKeyboardManager
    
    // New (v6.0)
    // [Note: The specific new replacement token was not provided in the source, but users should look for the updated singleton access pattern in the v6.0 API]
  8. Install IQKeyboardManagerSwift via Carthage

    master

    To integrate using Carthage, add the following line to your Cartfile:

    github "hackiftekhar/IQKeyboardManager"

    Then, run the following command to build the frameworks:

    carthage update --use-xcframeworks

    After building, drag IQKeyboardManagerSwift.xcframework into your Xcode project. Important: Only add one framework (either the Objective-C or Swift version), not both.

  9. Migrate from IQKeyboardManager 4.0 to 5.0

    master

    When upgrading from version 4.0 to 5.0, several properties and functions have been removed or replaced with a new API structure.

    Removed Properties

    • UIView.isAskingCanBecomeFirstResponder (No longer necessary)
    • IQKeyboardManager.shouldFixTextViewClip (Now handled internally)
    • IQKeyboardManager.canAdjustTextView (Now handled internally)
    • IQKeyboardManager.shouldAdoptDefaultKeyboardAnimation (No longer needed)

    Updated IQKeyboardManager API

    Functions for managing class-specific behaviors (distance handling, toolbars, etc.) have changed from imperative disable... and remove... functions to property-based management using arrays of classes.

    Updated UIView Category API

    Properties related to placeholder text and toolbar targets have been renamed or restructured.

  10. Enable the Keyboard Toolbar in IQKeyboardManager 8.0

    master

    In version 8.0, IQKeyboardManager.shared.enableAutoToolbar defaults to false because toolbar handling has been moved to the IQKeyboardToolbarManager library. To restore the toolbar functionality, use one of the following two methods:

    1. Via IQKeyboardManager: Set IQKeyboardManager.shared.enableAutoToolbar = true in your app delegate.
    2. Via IQKeyboardToolbarManager: Use the dedicated manager directly by setting IQKeyboardToolbarManager.shared.isEnabled = true.
    // Solution 1
    IQKeyboardManager.shared.enableAutoToolbar = true
    
    // Solution 2
    IQKeyboardToolbarManager.shared.isEnabled = true