MyStatusBar.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * 自定义状态栏(Android)
  3. * @邠心vbe on 2023/02/15
  4. */
  5. import React, { Component } from 'react';
  6. import { StatusBar } from 'react-native';
  7. let statusBar;
  8. export default class MyStatusBar extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. statusTheme: "dark-content", //themeStatusBar,
  13. statusColor: "transparent", //colorPrimaryDark,
  14. isTranslucent: true
  15. };
  16. statusBar = this;
  17. }
  18. static DARK_STYLE = "dark-content";
  19. static LIGHT_STYLE = "light-content";
  20. static DEFAULT_STYLE = themeStatusBar;
  21. /**
  22. * 设置状态栏颜色
  23. * @param {String} color 颜色值
  24. */
  25. static setStatusBarColor(color) {
  26. statusBar.setStatusBarColor(color);
  27. }
  28. /**
  29. * 设置状态栏主题(字体图标颜色)
  30. * @param {String} theme 主题:MyStatusBar.DARK_STYLE, MyStatusBar.LIGHT_STYLE
  31. */
  32. static setStatusBarTheme(theme) {
  33. statusBar.setStatusBarTheme(theme);
  34. }
  35. /**
  36. * 设置状态栏主题和颜色
  37. * @param {*} theme 主题:MyStatusBar.DARK_STYLE, MyStatusBar.LIGHT_STYLE
  38. * @param {*} color 颜色
  39. */
  40. static setStatusBarThemes(theme, color="transparent") {
  41. statusBar.setStatusBarThemes(theme, color);
  42. }
  43. /**
  44. * 设置透明状态栏
  45. */
  46. static setTranslucentStatusBar() {
  47. statusBar.setStatusBarThemes(MyStatusBar.DARK_STYLE, "transparent", true);
  48. }
  49. setStatusBarColor(color) {
  50. this.setState({
  51. statusColor: color
  52. })
  53. }
  54. setStatusBarTheme(theme) {
  55. this.setState({
  56. statusTheme: theme
  57. })
  58. }
  59. setStatusBarThemes(theme, color, trans=true) {
  60. this.setState({
  61. statusColor: color,
  62. statusTheme: theme,
  63. isTranslucent: trans
  64. })
  65. }
  66. render() {
  67. return (
  68. <StatusBar
  69. barStyle={this.state.statusTheme}
  70. translucent={this.state.isTranslucent}
  71. backgroundColor={this.state.statusColor}/>
  72. );
  73. }
  74. }