Swift 4中的statusBarStyle

时间:2018-03-27 06:23:27

标签: ios swift uinavigationbar statusbar

这是我在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
}

但是我的状态栏没有显示轻量级内容我不知道什么是ging,任何帮助?

5 个答案:

答案 0 :(得分:11)

对于特定的ViewController类:

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

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

这将在轻量级内容中显示您的状态栏

对于整个应用程序:

设置"查看基于控制器的状态栏外观"有价值的关键"否"在您产品的Info.plist

enter image description here

然后在AppDelegate.Swift里面使用以下代码" didFinishLaunchingWithOptions":

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UIApplication.shared.statusBarStyle = .lightContent
    return true
}

答案 1 :(得分:1)

选择您的项目并导航至Target -> General -> Deployment Info。其中Status Bar StyleDefault,请将其更改为Light

enter image description here

答案 2 :(得分:0)

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

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

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
 }

答案 3 :(得分:0)

在特定控制器的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;
    }

答案 4 :(得分:0)

  

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

在AppDelegate函数UIApplication.shared.statusBarStyle = .lightContent下手动设置-didFinishLaunchingWithOptions似乎无效

首先在文件Info.plist中设置

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

第二:

Target -> General -> Deployment Info

Status Bar Style设置为Light