Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyboardAvoidingView is not adjusting correctly because the coordinate system mismatch #33421

Closed
summer-wu opened this issue Mar 14, 2022 · 4 comments
Labels
API: Keyboard Component: KeyboardAvoidingView Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@summer-wu
Copy link

Description

You are comparing height using different coordinate systems.
The keyboard frame is in window system, and the view's frame is in parent view's coordinate system.
We have to convert the frame to the same coordinate system then do compare.
Here is how I fix it:

  _onLayout = (event: any) => {
    const measureCallback = (x: number, y: number, width: number, height: number) => {
      const wasFrameNull = this._frame == null;
      this._frame = { x, y, width, height };
      if (!this._initialFrameHeight) {
        // save the initial frame height, before the keyboard is visible
        this._initialFrameHeight = this._frame.height;
      }

      if (wasFrameNull) {
        this._updateBottomIfNecesarry();
      }
    };
    this.viewRef.current.measureInWindow(measureCallback);
  };

The buggy version:

  _onLayout = (event: ViewLayoutEvent) => {
    const wasFrameNull = this._frame == null;
    this._frame = event.nativeEvent.layout;
    if (!this._initialFrameHeight) {
      // save the initial frame height, before the keyboard is visible
      this._initialFrameHeight = this._frame.height;
    }

    if (wasFrameNull) {
      this._updateBottomIfNecesarry();
    }
  };

Version

0.66

Output of npx react-native info

don't needed

Steps to reproduce

don't needed

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

No response

@summer-wu summer-wu changed the title KeyboardAvoidingView is not adjusting correctly KeyboardAvoidingView is not adjusting correctly because the coordinate system mismatch Mar 14, 2022
@drice
Copy link

drice commented Apr 13, 2022

Possibly related, I'm seeing when keyboard is opened in landscape then rotated to portrait, the _initialFrameHeight is a different value than when it was opened in portrait so it doesn't resize correctly.

@HacerBusraKILIC
Copy link

HacerBusraKILIC commented May 16, 2022

I'm having trouble switching from landscape to portrait and from portrait to landscape with KeyboardAvoidingView on android. Any idea how can i solve it?

My code:

return (
          <KeyboardAvoidingView
                      behavior = { Platform.OS === 'ios' ? 'padding' : 'height' }
                       keyboardVerticalOffset = {
                          Platform.OS === 'ios'
                              ? iosVerticalOffset
                              : androidVerticalOffset
                      }
                      style= {{ flex: 1 }}>
             <View style={{ flex:1 }}>
                 <Header />
                 <Flatlist style={{ flex: 1 }}/>
                 <TextInput />
             </View>
          </KeyboardAvoidingView>
);
20220516145020096.mp4

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 13, 2022
@cortinico
Copy link
Contributor

Closing as stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: Keyboard Component: KeyboardAvoidingView Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

5 participants