【问题标题】:statusBarStyle in Swift 4Swift 4 中的状态栏样式
【发布时间】:2018-09-05 10:29:15
【问题描述】:

这是我在 AppDelegate 类 (Swift 4) 中编写的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
     UIApplication.shared.statusBarStyle = .lightContent
    goToRootViewController()
    UINavigationBar.appearance().isTranslucent = true

    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    GMSServices.provideAPIKey(googleMapApiKey)
    return true
}

但我的状态栏没有显示轻量级的内容,我不知道在做什么,有什么帮助吗?

【问题讨论】:

标签: ios swift uinavigationbar statusbar


【解决方案1】:

对于特定的 ViewController 类:

在你的 viewcontroller 类中使用下面的代码 viewDidLoad() :

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

这将以浅色内容显示您的状态栏

对于整个应用程序:

在产品的 Info.plist 中设置“查看基于控制器的状态栏外观”键,值为“NO”

然后在“didFinishLaunchingWithOptions”内的 AppDelegate.Swift 中使用以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    UIApplication.shared.statusBarStyle = .lightContent
    return true
}

【讨论】:

  • 是的,它只适用于特定的 ViewController。如果您想在 info.plist 中为整个应用设置“查看基于控制器的状态栏外观”键并将其指定为 NO,则只需在 AppDelegate.swift 中使用“UIApplication.shared.statusBarStyle = .lightContent”。你的问题会解决的。
  • 是的,但我在 appdelegate 中完成了,但在我的代码中不起作用
  • 您是否在 info.plist 中设置了“查看基于控制器的状态栏外观”键,值为“NO”
  • 我尝试过但不工作,这是来自 X 代码的建议:Setter for 'statusBarStyle' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]
【解决方案2】:

选择您的项目并导航到Target -> General -> Deployment Info。那Status Bar Style就是Default,改成Light

【讨论】:

  • 我已经这样做了,但没有任何改变它仍然显示黑暗
【解决方案3】:

“statusBarStyle”的设置器在 iOS 9.0 中已弃用:使用 -[UIViewController preferredStatusBarStyle]

在 AppDelegate 函数 -didFinishLaunchingWithOptions 下手动设置 UIApplication.shared.statusBarStyle = .lightContent 似乎不起作用

文件Info.plist中的第一个集合

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

第二个:

Target -> General -> Deployment Info

Status Bar Style 设置为Light

【讨论】:

    【解决方案4】:

    您是否更改了 info.plist 行 查看基于控制器的状态栏外观并将其设置为 NO ? 因为这是更改状态栏外观所必需的

    如果您只想更改特定视图控制器上的状态栏,那么您可以使用

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    【讨论】:

      【解决方案5】:

      在您的特定控制器的 didLoad 中使用以下代码

      UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
      
          if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
      
              statusBar.backgroundColor = [self colorWithHexString:@"C2BFAB"];//set whatever color you like
             // appDel.statusBarColor = NO;
          }
      

      【讨论】:

        猜你喜欢
        • 2018-11-12
        • 1970-01-01
        • 2016-12-09
        • 2020-01-09
        • 1970-01-01
        • 2016-01-15
        • 1970-01-01
        • 2019-06-27
        • 2017-12-13
        相关资源
        最近更新 更多