Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 740
Stanislau Buzunko Avatar asked Apr 08 '18 13:04

Stanislau Buzunko


3 Answers

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()
}
like image 193
Stanislau Buzunko Avatar answered Oct 22 '22 00:10

Stanislau Buzunko


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

like image 6
Prafull Sharma Avatar answered Oct 22 '22 01:10

Prafull Sharma


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

Change 'expo' to 'expo-constants'.

like image 2
codeinjuice Avatar answered Oct 21 '22 23:10

codeinjuice