uni app 横屏展示 报错uni.setScreenOrientation is not a function

解决方案

1. 首先检查 manifest.json 配置(最关键)

打开 manifest.json,切换到 App 启动界面配置,必须勾选所有你需要的屏幕方向

json
// manifest.json → app-plus → distribute → ios 或 android
{
  "app-plus": {
    "distribute": {
      "android": {
        "screenOrientation": [
          "portrait-primary",
          "portrait-secondary",
          "landscape-primary",
          "landscape-secondary"
        ]
      },
      "ios": {
        "screenOrientation": [
          "portrait-primary",
          "portrait-secondary",
          "landscape-primary",
          "landscape-secondary"
        ]
      }
    }
  }
}

可视化操作:

  1. 打开 manifest.json

  2. 点击 App 启动界面配置

  3. 在 屏幕方向 中,全部勾选(竖屏正反、横屏正反都要选)

  4. 保存文件

https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/screen-orientation.png


2. 正确的 App 端代码(使用 plus.screen.lockOrientation)

在 App 端,除了 uni.setScreenOrientation,更稳定的方式是使用 HTML5+ 的 plus.screen.lockOrientation

html
<template>
  <view class="content">
    <button type="primary" @click="toggleScreen">
      {{ isLandscape ? '切换竖屏' : '切换横屏' }}
    </button>
    <text>当前状态: {{ isLandscape ? '横屏' : '竖屏' }}</text>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isLandscape: false
    };
  },
  methods: {
    toggleScreen() {
      this.isLandscape = !this.isLandscape;
      
      // #ifdef APP-PLUS
      // App 端使用 plus.screen.lockOrientation
      const orientation = this.isLandscape ? 'landscape-primary' : 'portrait-primary';
      
      // 方式1: 使用 plus 原生 API(推荐)
      plus.screen.lockOrientation(orientation);
      
      // 方式2: 同时使用 uni 的 API(双保险)
      uni.setScreenOrientation({
        orientation: orientation,
        success: () => {
          console.log('切换成功');
        },
        fail: (err) => {
          console.error('切换失败', err);
          this.isLandscape = !this.isLandscape;
          uni.showToast({ title: '切换失败,请检查配置', icon: 'none' });
        }
      });
      // #endif
    }
  }
};
</script>

3. 如果依然报错,使用 H5+ 的 plus 对象检测

有时候 Uni App 的 API 映射会有延迟,可以用 plus 直接调用:

javascript
toggleScreen() {
  this.isLandscape = !this.isLandscape;
  
  // #ifdef APP-PLUS
  if (typeof plus !== 'undefined' && plus.screen) {
    const orientation = this.isLandscape ? 'landscape-primary' : 'portrait-primary';
    plus.screen.lockOrientation(orientation);
    console.log('使用 plus.screen.lockOrientation 切换成功');
  } else {
    // 备用方案
    uni.setScreenOrientation({
      orientation: this.isLandscape ? 'landscape-primary' : 'portrait-primary'
    });
  }
  // #endif
}

4. 完整的 App 端横屏切换示例(含防抖)

html
<template>
  <view class="container">
    <view class="status-icon">{{ isLandscape ? '🌅' : '📱' }}</view>
    <view class="status-text">当前:{{ isLandscape ? '横屏模式' : '竖屏模式' }}</view>
    
    <button class="toggle-btn" @click="toggleScreen" :disabled="isLoading">
      {{ isLoading ? '切换中...' : (isLandscape ? '切换到竖屏' : '切换到横屏') }}
    </button>
    
    <view class="tips">
      <text>⚠️ 提示:请确保 manifest.json 中已勾选所有屏幕方向</text>
      <text>📌 App 端测试请使用真机或自定义基座</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isLandscape: false,
      isLoading: false
    };
  },
  methods: {
    toggleScreen() {
      if (this.isLoading) return;
      
      this.isLoading = true;
      const targetLandscape = !this.isLandscape;
      
      // #ifdef APP-PLUS
      try {
        // 优先使用 plus.screen
        if (typeof plus !== 'undefined' && plus.screen) {
          const orientation = targetLandscape ? 'landscape-primary' : 'portrait-primary';
          plus.screen.lockOrientation(orientation);
          this.isLandscape = targetLandscape;
          this.isLoading = false;
          uni.showToast({
            title: targetLandscape ? '已切换横屏' : '已切换竖屏',
            icon: 'success'
          });
        } else {
          // 备用:使用 uni API
          uni.setScreenOrientation({
            orientation: targetLandscape ? 'landscape-primary' : 'portrait-primary',
            success: () => {
              this.isLandscape = targetLandscape;
              this.isLoading = false;
              uni.showToast({
                title: targetLandscape ? '已切换横屏' : '已切换竖屏',
                icon: 'success'
              });
            },
            fail: (err) => {
              console.error('切换失败:', err);
              this.isLoading = false;
              uni.showToast({
                title: '切换失败,请检查 manifest 配置',
                icon: 'none'
              });
            }
          });
        }
      } catch (e) {
        console.error('切换异常:', e);
        this.isLoading = false;
        uni.showToast({
          title: '切换异常: ' + e.message,
          icon: 'none'
        });
      }
      // #endif
    }
  },
  mounted() {
    // 获取当前屏幕方向(可选)
    // #ifdef APP-PLUS
    try {
      // 通过屏幕宽高判断当前方向
      const width = plus.screen.resolutionWidth;
      const height = plus.screen.resolutionHeight;
      this.isLandscape = width > height;
    } catch (e) {
      console.log('获取屏幕方向失败', e);
    }
    // #endif
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 40rpx;
  background: #f5f6fa;
}
.status-icon {
  font-size: 120rpx;
  margin-bottom: 20rpx;
}
.status-text {
  font-size: 40rpx;
  color: #2d3436;
  margin-bottom: 60rpx;
  font-weight: bold;
}
.toggle-btn {
  width: 500rpx;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 32rpx;
  border-radius: 45rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  margin-bottom: 60rpx;
}
.toggle-btn[disabled] {
  opacity: 0.6;
}
.tips {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #636e72;
  font-size: 24rpx;
  line-height: 2;
}
</style>

5. 如果还是不行,检查以下几点

 
检查项操作
是否使用自定义基座 标准基座可能不支持屏幕旋转,需运行到 自定义基座
是否真机调试 模拟器可能不支持,用真机测试
manifest 是否配置正确 确认所有方向都勾选了
是否重新编译 修改 manifest 后需要 重新编译 或 重新打包
Android 权限 检查 AndroidManifest.xml 是否有 android:screenOrientation 配置

6. 最终终极方案(如果上述都不行)

在 App 端,直接调用 Android/iOS 原生方法(需要 uni-app 的 Native.js):

javascript
// Android 示例
// #ifdef APP-PLUS
if (plus.os.name === 'Android') {
  const activity = plus.android.runtimeMainActivity();
  activity.setRequestedOrientation(0); // 0=横屏, 1=竖屏
}
// #endif

但一般情况下,使用 plus.screen.lockOrientation 就能解决问题。


总结

最推荐的做法:

  1. 在 manifest.json 中勾选所有屏幕方向

  2. 使用 plus.screen.lockOrientation 而不是 uni.setScreenOrientation

  3. 重新编译运行到自定义基座或真机

posted @ 2026-07-03 09:38  张筱菓  阅读(35)  评论(0)    收藏  举报