uni-app 程序启动设置为高亮时,手机设置为暗黑模式无法启动

uni-app 程序启动设置为高亮时,手机设置为暗黑模式无法启动

类别 信息
产品分类 uniapp/App
PC开发环境 Mac
PC版本号 14.2.1 (23C71)
HBuilderX类型 正式
HBuilderX版本号 3.99
手机系统 iOS
手机系统版本号 iOS 16
手机厂商 苹果
手机机型 13pm
页面类型 vue
vue版本 vue2
打包方式 离线
项目创建方式 HBuilderX

bug描述:

onShow: function(ops) {  
    // #ifdef  APP-PLUS  
    // plus.nativeUI.setUIStyle('light');  
    // #endif  
},  

在App.vue 启动的时候设置为高亮模式,把手机的暗黑模式开启成黑色后,程序无法启动报错
```
0   CoreFoundation  
___exceptionPreprocess + 164  
1   libobjc.A.dylib  
_objc_exception_throw + 60  
2   Foundation  
__userInfoForFileAndLine  
3   UIKitCore  
-[UIApplication _performAfterCATransactionCommitsWithLegacyRunloopObserverBasedTiming:block:] + 396  
4   UIKitCore  
-[_UIViewControllerNullAnimationTransitionCoordinator _runAlongsideCompletionsAfterCommit] + 112  
5   UIKitCore  
-[UIWindow _setTraitCollectionChangeTransitionCoordinator:] + 68  
6   UIKitCore  
-[UIWindow _updateWindowTraitsAndNotify:] + 316  
7   UIKitCore  
-[_UITraitOverrides _setOverrideTraitCollection:defaultValueOverrides:] + 236  
8   UIKitCore  
-[_UITraitOverrides _setNSIntegerValue:forTraitToken:] + 296  
9   UIKitCore  
-[UIView(UserInterfaceStyle) setOverrideUserInterfaceStyle:] + 48  
10  HBuilder  
-[PGNativeUI setUiStyle:] + 392  
11  HBuilder  
-[PDRCoreFeature Execute:] + 288  
12  HBuilder  
-[PDRCoreApp handleCmds:] + 104  
13  HBuilder  
-[PDRCoreAppFrame webViewEnginde:didReceiveSyncScriptMessage:] + 108  
14  HBuilder  
-[H5WeexWebview handleMessage:withObject:] + 148  
15  HBuilder  
__28-[H5WeexWebview refreshWeex]_block_invoke + 60  
16  HBuilder  
-[WXSDKInstance(DCPlusBridge) execSync:] + 96  
17  HBuilder  
-[PlusWeexModule execSync:] + 60  
18  CoreFoundation  
___invoking___ + 148  
19  CoreFoundation  
-[NSInvocation invoke] + 428

更多关于uni-app 程序启动设置为高亮时,手机设置为暗黑模式无法启动的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

这个问题下个版本修复,setUiStyle先不要在app.vue内调用 去首页调

更多关于uni-app 程序启动设置为高亮时,手机设置为暗黑模式无法启动的实战教程也可以访问 https://www.itying.com/category-93-b0.html


uni-app 中,如果你将应用程序的启动设置为高亮(即使用亮色主题),而用户的手机系统设置为暗黑模式,可能会导致应用程序无法正常启动或显示异常。这是因为暗黑模式和高亮模式之间的冲突。

要解决这个问题,你可以采取以下几种方法:

1. 动态适配系统主题

你可以通过检测系统的主题模式,动态地调整应用程序的主题。uni-app 提供了 uni.getSystemInfo 方法,可以获取系统的主题信息。

uni.getSystemInfo({
  success: function (res) {
    if (res.theme === 'dark') {
      // 系统为暗黑模式,应用暗黑主题
      document.body.style.backgroundColor = '#000';
      document.body.style.color = '#fff';
    } else {
      // 系统为亮色模式,应用亮色主题
      document.body.style.backgroundColor = '#fff';
      document.body.style.color = '#000';
    }
  }
});

2. 使用 uni-apptheme 配置

pages.json 中,你可以配置 theme 来适配不同的主题模式。

{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",
        "theme": "light" // 或者 "dark"
      }
    }
  ]
}

3. 使用 CSS 变量

你可以使用 CSS 变量来定义不同的主题颜色,并根据系统主题动态切换。

:root {
  --background-color: #fff;
  --text-color: #000;
}

[data-theme="dark"] {
  --background-color: #000;
  --text-color: #fff;
}

body {
  background-color: var(--background-color);
  color: var(--text-color);
}

然后在 JavaScript 中动态切换主题:

uni.getSystemInfo({
  success: function (res) {
    if (res.theme === 'dark') {
      document.documentElement.setAttribute('data-theme', 'dark');
    } else {
      document.documentElement.setAttribute('data-theme', 'light');
    }
  }
});

4. 使用 uni-app 插件

你也可以使用一些 uni-app 插件来帮助管理主题,例如 uni-ui 中的 uni-theme 组件。

5. 强制使用亮色主题

如果你希望无论系统设置如何,都强制使用亮色主题,可以在 App.vueonLaunch 方法中设置:

export default {
  onLaunch: function() {
    uni.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });
    uni.setTabBarStyle({
      color: '#000000',
      selectedColor: '#000000',
      backgroundColor: '#ffffff'
    });
  }
}

6. 使用 manifest.json 配置

manifest.json 中,你可以配置 app-plustheme 来强制使用亮色主题:

{
  "app-plus": {
    "theme": "light"
  }
}
回到顶部