2024.2.15.
GeunYeong Kim
height: Dimensions.get('window').height;
bottomSheet를 구현하다가 위 코드가 iOS와 안드로이드에서 다르게 동작하는 문제가 발생했다.
React Native에서는 vw, vh 등을 사용할 수 없기 때문에 Screen size를 구하기 위해 Dimension을 사용할 수 있다.
Dimension의 get에는 ‘window’와 ‘screen’의 두 가지 파라미터를 줄 수 있다.
const windowWidth = Dimensions.get('window').width;
const windowHeight = Dimensions.get('window').height;
const screenWidth = Dimensions.get('screen').width;
const screenHeight = Dimensions.get('screen').height;
두 가지의 차이에 대해서공식문서에는 다음과 같이 정리되어 있는데, 제대로 이해하기는 어렵다.
관련한 이슈도 있지만 아직 개선되지는 않은 상황이다.
이와 관련된 스택오버플로우 답변에서 자세한 대답을 찾을 수 있었다.
소스의 Dimension 부분을 살펴보면 주석으로 다음과 같이 적혀있는 것을 볼 수 있다.
// Screen and window dimensions are different on android
애초에 높이 계산 로직 자체를 다르게 해줘야하는 것 같았다.
아래 다른 답변을 확인해보니, 안드로이드에서 지원하는 여러 기능으로 인해 iOS와는 다르게 window 사이즈가 screen 사이즈보다 작거나 같을 수 밖에 없는 현상이 이해되었다.
따라서 다른 스택오버플로우 답변에서 알 수 있듯이, 안드로이드의 window 높이에는 iOS와는 다르게 ‘하단 내비게이션 바 높이가 포함될 가능성’이 존재하기 때문에 다르게 동작하는 것이었다.
If you are targeting foldable devices or devices which can change the screen size or app window size, you can use the event listener available in the Dimensions module as shown in the below example.
const [dimensions, setDimensions] = useState({
window: windowDimensions,
screen: screenDimensions,
});
useEffect(() => {
const subscription = Dimensions.addEventListener(
'change',
({window, screen}) => {
setDimensions({window, screen});
},
);
return () => subscription?.remove();
});
끝