如何在互联网可达性上使用 ios 快速更改状态栏颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27124214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:47:57  来源:igfitidea点击:

How to Change the status bar color using ios with swift on internet reachability?

iosswiftstatusbar

提问by Fatti Khan

i want to change the color of my device status bar if the internet is connected than the status bar color should turn Black and if the internet is not connected the color or status bar should turn Red so that it indicates wether internet is working or not during working with the application using SWIFT...help me out

如果已连接互联网,我想更改设备状态栏的颜色,而不是状态栏颜色应变为黑色,如果未连接互联网,则颜色或状态栏应变为红色,以表明在此期间互联网是否正常工作使用 SWIFT 处理应用程序...帮助我

回答by Nikita Khandelwal

In your Info.plistyou need to set "View controller-based status bar appearance" to a boolean value.

在您Info.plist需要将“基于视图控制器的状态栏外观”设置为布尔值。

If you set it to YESthen you should override preferredStatusBarStylefunction in each view controller.

如果您将其设置为YES那么您应该覆盖preferredStatusBarStyle每个视图控制器中的功能。

If you set it to NOthen you can set the style in AppDelegateusing:

如果将其设置为,NO则可以AppDelegate使用以下方式设置样式:

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

回答by A.G

override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden =  true

    //Status bar style and visibility
    UIApplication.sharedApplication().statusBarHidden = false
    UIApplication.sharedApplication().statusBarStyle = .LightContent

    //Change status bar color
    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector("setBackgroundColor:") {
        statusBar.backgroundColor = UIColor.redColor()
    }

}

回答by Cody

Tested in Swift & iOS9

在 Swift 和 iOS9 中测试

If you use Navigation Controllers, put this in your viewcontroller class:

如果您使用Navigation Controllers,请将其放在您的 viewcontroller 类中:

override func viewDidLoad(){
    ...
    self.navigationController?.navigationBar.barStyle = .Black
}

Otherwise, override the preferredStatusBarStyle()in your UIViewController:

否则,覆盖preferredStatusBarStyle()UIViewController 中的:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

You could find more information here

你可以在这里找到更多信息

回答by Taichi Kato

For Swift 3

对于 Swift 3

This should work for Xcode 8 and Swift 3

这应该适用于 Xcode 8 和 Swift 3

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

回答by Kevin Machado

For Swift 2.3

对于 Swift 2.3

Try with these methods

试试这些方法

// Get network status
class func hasConnectivity() -> Bool {
    let reachability: Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus: Int = reachability.currentReachabilityStatus().value
    return networkStatus != 0
}

// change status bar color
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.blueColor()
navigationBarAppearace.barTintColor = UIColor.blueColor()

tintColorattribute change the background color of the navigation bar

tintColor属性改变导航栏的背景颜色

barTintColorattribute affect to the color of the

barTintColor将影响归因于颜色

But if you want to change the status bar color at the runtime, I think the better way is adding a view behind your status bar.

但是如果您想在运行时更改状态栏颜色,我认为更好的方法是在状态栏后面添加一个视图

回答by Shanmugasundharam

// Within your AppDelegate.swift in didFinishLaunchingWithOptions: UINavigationBar.appearance().barTintColor = UIColor.greenColor()

// 在你的 AppDelegate.swift 中的 didFinishLaunchingWithOptions: UINavigationBar.appearance().barTintColor = UIColor.greenColor()

//Optionally, if you need a specific color, how you do it with RGB:
UINavigationBar.appearance().barTintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

                                or 

In your Info.plist you need to set "View controller-based status bar appearance" to a boolean value.

在您的 Info.plist 中,您需要将“基于视图控制器的状态栏外观”设置为布尔值。

 UIApplication.sharedApplication().statusBarStyle = .LightContent

回答by Yuvrajsinh

As @rckoenes commented as of iOS 7 the status bar is draw over your app. So you can put a view behind the status bar area (20px from top - height of status bar) and can control it's background colour as per internet connection status changes, there is no other option you to change status bar colour.

正如@rckoenes 所评论的,从 iOS 7 开始,状态栏被绘制在您的应用程序上。因此,您可以在状态栏区域后面放置一个视图(距离顶部 20 像素 - 状态栏的高度),并且可以根据互联网连接状态的变化来控制它的背景颜色,没有其他选项可以更改状态栏颜色。

回答by Anup G Prasad

To have white text in black status bar: Switch View controller-based status bar appearanceto NOin Info.plistIn AppDelegate.swift add let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView statusBar.backgroundColor = UIColor.blackin didFinishLaunchingWithOptions

要在黑色状态栏中显示白色文本:在Info.plist中将基于视图控制器的状态栏外观切换为NO在 AppDelegate.swift 中添加 didFinishLaunchingWithOptionslet statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView statusBar.backgroundColor = UIColor.black

回答by Rahul Patel

UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)