react-native-keyboard-manager

repository·main·Indexed 21 days ago

https://github.com/douglasjunior/react-native-keyboard-manager

An iOS-only library for React Native that prevents the keyboard from covering text inputs by integrating the IQKeyboardManager Swift library. It provides the KeyboardManager API for customizing keyboard interaction and a PreviousNextView component for keyboard navigation in modals. Note: This library is currently in maintenance mode.

Tokens
3.1K
Snippets
12
Records
15
Agent score
74%

What's inside react-native-keyboard-manager

  1. ⚠️ DEPRECATION NOTICE ⚠️

    main

    This library is in maintenance mode and is no longer receiving new features. It was originally designed to solve iOS-only keyboard issues by integrating IQKeyboardManager.

    Because Android now handles edge-to-edge layouts by default, this library's iOS-only focus is less relevant.

    Recommended Alternatives:

    • KeyboardAvoidingView (React Native built-in)
    • react-native-keyboard-controller (Cross-platform edge-to-edge approach)
  2. Reload the application to see changes

    main

    After modifying your code (e.g., in App.tsx), use the following shortcuts to reload the app and see your changes:

    • Android: Press the <kbd>R</kbd> key twice, or open the Developer Menu using <kbd>Ctrl</kbd> + <kbd>M</kbd> (Windows/Linux) or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> (macOS) and select "Reload".
    • iOS: Press <kbd>Cmd ⌘</kbd> + <kbd>R</kbd> in the iOS Simulator.
  3. Install react-native-keyboard-manager

    main

    Follow these steps to install the library for iOS. Note that this library is iOS only. For Android, use android:windowSoftInputMode="adjustResize" in your activity configuration.

    Version Compatibility

    • RN 0.72.0 or later: use react-native-keyboard-manager@latest
    • RN 0.60.0 ... 0.71.X: use react-native-keyboard-manager@6.5.11-2
    • RN 0.53.0 ... 0.59.10: use react-native-keyboard-manager@4.0.13-12
    • RN 0.47.0 ... 0.52.2: use react-native-keyboard-manager@4.0.13-5
    • RN 0.40.0 ... 0.46.4: use react-native-keyboard-manager@4.0.13-1
    # 1. Install JS dependency
    yarn add react-native-keyboard-manager
    # OR
    npm i -S react-native-keyboard-manager
    
    # 2. Add CocoaPods dependency to ios/Podfile
    # Use the specific fork to solve PrivacyInfo.xcprivacy issues
    pod 'IQKeyboardManagerSwift', :git => 'https://github.com/douglasjunior/IQKeyboardManager.git', :branch => 'react-native-keyboard-manager'
    
    # 3. Install Pods
    cd ios
    pod install
  4. Enable Swift in your Xcode project

    main

    Since IQKeyboardManager is written in Swift, you must enable Swift in your native Xcode project to avoid build errors.

    1. Open ios/YourAppName.xcworkspace in Xcode.
    2. Right-click on your project in the Project Navigator and select New File.
    3. Create a single empty Swift file (ensure your app target is selected).
    4. When prompted, click Create Bridging Header.
    5. Do not remove the Swift file.
    6. Re-run your build.
  5. Use Next/Previous buttons in Modals

    main

    To use Next/Previous buttons on the keyboard toolbar, call KeyboardManager.setToolbarPreviousNextButtonEnable(true).

    If you are using these buttons inside a Modal, you must wrap your TextInput fields within a PreviousNextView component to ensure correct behavior.

    import { PreviousNextView } from 'react-native-keyboard-manager';
    
    class App extends Component {
      render() {
          return (
              <View> 
                  <Modal> 
                      <PreviousNextView style={...} >
                          {/* All TextInput components go here */}
                      </PreviousNextView>
                  </Modal>
              </View>
          );
      }
    }
  6. Configure KeyboardManager API

    main

    The KeyboardManager API allows you to customize how the keyboard interacts with your text inputs on iOS. Most configurations should be wrapped in a check for Platform.OS === 'ios'.

    import { Platform } from 'react-native';
    import KeyboardManager from 'react-native-keyboard-manager';
    
    if (Platform.OS === 'ios') {
        KeyboardManager.setEnable(true);
        KeyboardManager.setEnableDebugging(false);
        KeyboardManager.setKeyboardDistanceFromTextField(10);
        KeyboardManager.setLayoutIfNeededOnUpdate(true);
        KeyboardManager.setEnableAutoToolbar(true);
        KeyboardManager.setToolbarDoneBarButtonItemText("Done");
        KeyboardManager.setToolbarManageBehaviourBy("subviews"); // "subviews" | "tag" | "position"
        KeyboardManager.setToolbarPreviousNextButtonEnable(false);
        KeyboardManager.setToolbarTintColor('#0000FF'); // Only #000000 format is supported
        KeyboardManager.setToolbarBarTintColor('#FFFFFF'); // Only #000000 format is supported
        KeyboardManager.setShouldShowToolbarPlaceholder(true);
        KeyboardManager.setOverrideKeyboardAppearance(false);
        KeyboardManager.setKeyboardAppearance("default"); // "default" | "light" | "dark"
        KeyboardManager.setShouldResignOnTouchOutside(true);
        KeyboardManager.setShouldPlayInputClicks(true);
        KeyboardManager.resignFirstResponder();
        
        KeyboardManager.isKeyboardShowing().then((isShowing) => {
            // ...
        });
    }
  7. Use KeyboardManager to configure keyboard behavior

    main

    The KeyboardManager object is the primary interface for controlling how the keyboard interacts with your React Native application. It provides methods to enable/disable management, adjust toolbar appearance, and control keyboard distance. Most methods are synchronous and affect the global keyboard management state.

    import KeyboardManager from 'react-native-keyboard-manager';
    
    // Enable keyboard management
    KeyboardManager.setEnable(true);
    
    // Set distance between keyboard and text field
    KeyboardManager.setKeyboardDistanceFromTextField(20);
    
    // Configure toolbar text
    KeyboardManager.setToolbarDoneBarButtonItemText('Done');
    
    // Check if keyboard is visible
    const isShowing = await KeyboardManager.isKeyboardShowing();
  8. Reference: KeyboardManager API methods

    main

    The following methods are available on the KeyboardManager object:

    setEnable(enable: boolean): void
    setEnableDebugging(enable: boolean): void
    setLayoutIfNeededOnUpdate(enable: boolean): void
    setKeyboardDistanceFromTextField(distance: number): void
    setEnableAutoToolbar(enable: boolean): void
    setToolbarDoneBarButtonItemText(text: string): void
    setToolbarManageBehaviourBy(behaviour: ToolbarBehaviour): void
    setToolbarPreviousNextButtonEnable(enable: boolean): void
    setToolbarTintColor(hexColor: string): void
    setToolbarBarTintColor(hexColor: string): void
    setShouldShowToolbarPlaceholder(enable: boolean): void
    setOverrideKeyboardAppearance(enable: boolean): void
    setKeyboardAppearance(appearance: KeyboardAppearance): void
    setShouldResignOnTouchOutside(enable: boolean): void
    setShouldPlayInputClicks(enable: boolean): void
    setShouldToolbarUsesTextFieldTintColor(enable: boolean): void
    resignFirstResponder(): void
    reloadLayoutIfNeeded(): void
    isKeyboardShowing(): Promise<boolean>