React native detect IOS status bar height when calling / sharing hotspot?

筅森魡賤 提交于 2019-12-30 10:27:13

问题


Status bar height changes when calling or sharing personal hotspot on ios and overlaps view, how to detect status bar height when it changes?


回答1:


I've faced this challenge and haven't found answers on stackoverflow / github issues.

I've come up with my own solution, and post it so this can save some time for others.

import { NativeModules, StatusBarIOS } from 'react-native'
const { StatusBarManager } = NativeModules

componentDidMount () {
  if (Platform.OS === 'ios') {
    StatusBarManager.getHeight(response =>
        this.setState({statusBarHeight: response.height})
    )

    this.listener = StatusBarIOS.addListener('statusBarFrameWillChange',
      (statusBarData) =>
        this.setState({statusBarHeight: statusBarData.frame.height})
    )
  }
}

componentWillUnmount () {
  if (Platform.OS === 'ios' && this.listener) {
    this.listener.remove()
  }
}

How it works

1) get initial height, note it's async method

StatusBarManager.getHeight(response =>
    this.setState({statusBarHeight: response.height}))

2) setup listener for status bar change

StatusBarIOS.addListener('statusBarFrameWillChange',
    (statusBarData) =>
      this.setState({statusBarHeight: statusBarData.frame.height})
  )

3) remove listener in componentWillUnmount

if (Platform.OS === 'ios' && this.listener) {
  this.listener.remove()
}



回答2:


You can use react-native-status-bar-height library to get height of status bar, and can use like

marginTop: getStatusBarHeight()

or you can use as a height of StatusBar, if its set to translucent.

If you are using Expo

then just import { Constants } from 'expo'

and height will be Constants.statusBarHeight

See Ref Here and here for Expo




回答3:


Now it's import Constants from 'expo-constants';

Change 'expo' to 'expo-constants'.



来源:https://stackoverflow.com/questions/49718503/react-native-detect-ios-status-bar-height-when-calling-sharing-hotspot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!