简体   繁体   中英

Dismiss Keyboard React-Native

Is there a simple way to hide the iOS keyboard? I want to force it shut in a few specific scenarios that don't necessarily require focusing another view. ie like a specific response from the server.

Use built-in Keyboard Module:

import { Keyboard } from 'react-native';

Keyboard.dismiss(); 

Was able to achieve this with

import dismissKeyboard from 'react-native/Libraries/Utilities/dismissKeyboard'

And then at the point where I need to toggle the keyboard off

dismissKeyboard();

-- -- EDIT -- --

Importing like this works equally as well.

 import dismissKeyboard from 'dismissKeyboard'

-- -- EDIT #2 -- --

My original answer is now outdated. The correct way is mentioned below by @Chen-Tsu Lin

import { Keyboard } from 'react-native'; Keyboard.dismiss();

You may also use this library I wrote:react-native-dismiss-keyboard . It does basically the same as the import of 'dismissKeyboard', but it only uses public API and is therefore safer to use and does not rely on the packager resolving 'dismissKeyboard' as it currently does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM