【问题标题】:can we config the screen orientation for particular screen in React Native?我们可以在 React Native 中为特定屏幕配置屏幕方向吗?
【发布时间】:2021-10-06 00:13:57
【问题描述】:

我是反应原生的新手。我陷入了一个问题,是否可以在 react-native 中锁定 Landscape 中的特定屏幕?换句话说,我们可以为特定屏幕配置屏幕方向吗?

帮我解决这个问题?

【问题讨论】:

    标签: react-native orientation fullscreen screen-orientation


    【解决方案1】:

    您可以使用react-native-orientationreact-native-orientation-locker

    https://www.npmjs.com/package/react-native-orientation https://www.npmjs.com/package/react-native-orientation-locker

    React 本机方向提供了以下 API,有助于实现屏幕方向

    Orientation.lockToPortrait()
    Orientation.lockToLandscape()
    Orientation.lockToLandscapeLeft()
    Orientation.lockToLandscapeRight()
    Orientation.unlockAllOrientations()
    Orientation.getOrientation((err, orientation) => {})
    Orientation.getSpecificOrientation((err, specificOrientation) => {})
    

    请检查以下代码sn-p

    import Orientation from 'react-native-orientation'
    
    componentDidMount() {
       Orientation.unlockAllOrientations();
    }
    
    componentWillUnmount() {
       Orientation.lockToPortrait();
    }
    

    【讨论】:

    • 谢谢你的回答,如果是功能组件,它会起作用吗?我可以为此使用使用效果吗?
    • 是的,您可以使用 useEffect 作为生命周期方法。它也适用于功能组件。
    猜你喜欢
    • 2021-12-22
    • 2018-03-19
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2022-11-13
    相关资源
    最近更新 更多