question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyboardAvoidingView misplaces absolutely positioned elements

See original GitHub issue

Description

Having an element with position: absolute in a KeyboardAvoidingView with behavior="padding and keyboardVerticalOffset greater than 0, moves the element permanently (even after closing the keyboard).

React Native version:

System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
    Memory: 727.96 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node
    Yarn: 1.21.1 - ~/.yarn/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.1/bin/npm
    Watchman: 4.7.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.1, DriverKit 19.0, macOS 10.15, tvOS 13.0, watchOS 6.0
    Android SDK:
      API Levels: 23, 25, 26, 27, 28
      Build Tools: 23.0.1, 25.0.2, 26.0.1, 27.0.3, 28.0.3
      System Images: a...google_apis | Google APIs Intel x86 Atom Sys...
      Android NDK: 17.0.4754217
  IDEs:
    Android Studio: 3.4 AI-183.6156.11.34.5522156
    Xcode: 11.1/11A1027 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_102 - /usr/bin/javac
    Python: 2.7.17 - /usr/local/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0
    react-native: 0.62.2 => 0.62.2
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. Run app
  2. Tap on text input
  3. Close text input

Expected Results

After closing the keyboard the element should be at its original position.

Snack, code example, screenshot, or link to a repository:

link to snack: https://snack.expo.io/@vmark/keyboardavoiding-absolute-positioning-bug link to repo: https://github.com/vujevits/KeyboardAvoidingAbsoluteExample

Screenshots

Screenshot 2020-04-20 at 15 33 04 Screenshot 2020-04-20 at 15 33 14 Screenshot 2020-04-20 at 15 33 20

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:13

github_iconTop GitHub Comments

15reactions
andrisole92commented, Dec 4, 2021

This is total shit my friends! I can’t believe it’s still there!

1reaction
AndresdoSantoscommented, Jan 30, 2021

Hello guys, I solved this by taking the ‘event’ and checking if the keyboard was ‘visible’ or ‘hidden’, put it in a state and did the logic.

const [keyboard, setKeyboard] = useState(Boolean);

  useEffect(() => {
    Keyboard.addListener('keyboardDidShow', _keyboardDidShow);
    Keyboard.addListener('keyboardDidHide', _keyboardDidHide);

    return () => {
      Keyboard.removeListener('keyboardDidShow', _keyboardDidShow);
      Keyboard.addListener('keyboardDidHide', _keyboardDidHide);
    };
  }, []);

  const _keyboardDidShow = () => {
    setKeyboard(true);
  };

  const _keyboardDidHide = () => {
    setKeyboard(false);
  };

and…

{!keyboard && (
    <View style={{ position: 'absolute', bottom: 0, height: 60 }} >
      <Text style={{ alignSelf: 'center' }} >Criar uma conta</Text>
    </View>
  )}

To be honest, I didn’t like the solution 100% because of the size of adding code for something so ‘simple’, but that’s what I did.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Button wtih absolute position inside KeyboardAvoidingView ...
I have a button with absolute position, used for a form. When inside a KeyboardAvoidingView it goes on top of the other elements...
Read more >
KeyboardAvoidingView not working properly? Check your ...
This is because Flexbox has default justifyContent as flex-start , which means items are positioned from the beginning(top) of the container ...
Read more >
ReactNative How to keep the scroll view position when the ...
Coding example for the question ReactNative How to keep the scroll view position when the keyboard opened-React Native.
Read more >
nested textinput on scrollview react native - You.com | The AI ...
Is there a way to keep the keyboard open even when input loses focus/prevent blur on ... height vs position vs padding in...
Read more >
Tweets with replies by Fernando Rojo ... - Twitter
A collection of high-quality, unstyled components for creating beautiful emails. ... no. a much more important feature is css object-position, which native ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found