Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1338 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

#21
**Swift 3**

In your `AppDelegate` file inside `func application` method


let statusBar: UIView = application.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = .red
Reply

#22
for swift 3

.plist

View controller-based status bar appearance = NO

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Custom statubar
UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = .lightContent
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = UIColor.gray

return true
}

Reply

#23
After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

**First Step:**

Open your info.plist and insert a new key named "*View controller-based status bar appearance*" to ***NO***

**Second Step (Just an explanation, no need to implement this):**

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,

***Swift 2***

UIApplication.sharedApplication().statusBarStyle = .LightContent

***Swift 3***

UIApplication.shared.statusBarStyle = .lightContent

but that *affects the `statusBarStyle` of all the ViewControllers.*

**So, how to get this working for specific ViewControllers - Final Step:**

Open the viewcontroller file where you want to change the `statusBarStyle` and put the following code in `viewWillAppear()`,

***Swift 2***

UIApplication.sharedApplication().statusBarStyle = .LightContent

***Swift 3***

UIApplication.shared.statusBarStyle = .lightContent


Also, implement the `viewWillDisappear()` method for that specific viewController and put the following lines of code,

***Swift 2***

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

***Swift 3***

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

This step will first change the `statusBarStyle` for the specific viewcontroller and then change it back to `default` when the specific viewcontroller disappears. Not implementing the `viewWillDisappear()` will change the `statusBarStyle` permanently to the new defined value of `UIStatusBarStyle.LightContent`
Reply

#24
I had some trouble with this one. I didn't really feel good about globally changing the status bar color in view did appear and then changing it back on view did disappear like the accepted answer. Believe it or not you can get this working by overriding `preferredStatusBarStyle` on your desired view controller. After much time this is what I did to get it working:

1. Change View controller-based status bar appearance in your `info.plist` to YES.
2. Now any full screen view controller can change the status bar style by overriding `preferredStatusBarStyle`.
3. I specify full screen because this will not work for (non-full screen) modal view controllers, not without setting `modal​Presentation​Captures​Status​Bar​Appearance` to Yes that is.
4. Also if you have embedded view controllers, like in a navigation controller for example, it will ask the top most view controller for status bar style. Overriding `child​View​Controller​For​Status​Bar​Style` and passing the embedded view controller is supposed to work but it didn't for me. So I just returned the embedded view controllers preferred status bar as the preferred status bar style. Something like this:

override var preferredStatusBarStyle: UIStatusBarStyle {
if let topViewController = viewControllers.last {
return topViewController.preferredStatusBarStyle
}

return .default
}
Reply

#25
There's a billion answers here so I thought why not add another in the form of an **extension** (with help from @Cœur)

## Swift 3

### Extension:

extension UIApplication {
class var statusBarBackgroundColor: UIColor? {
get {
return (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor
} set {
(shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = newValue
}
}
}

### Implementation:

UIApplication.statusBarBackgroundColor = .blue
Reply

#26
**Swift 3**

//
// LoginController.swift
// Swift 3
//
// Created by The Crab on 17/01/2017.
// Copyright © 2017 Paxi Labs. All rights reserved.
//

import UIKit

class LoginController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

setNeedsStatusBarAppearanceUpdate()

view.backgroundColor = UIColor(red: 61/255, green: 91/255, blue: 151/255, alpha: 1)

}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

Reply

#27
Swift 3.0 Update

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UIApplication.shared.statusBarStyle = .lightContent

return true
}
Reply

#28
**Swift 3**

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.black
}

That's the solution for setting background color of the status bar for specific view controller.
Reply

#29
I can suggest you a simpler way,

1. Just call [setNeedsStatusBarAppearanceUpdate][1] in viewDidLoad as Apple docs says,

> Call this method if the view controller's status bar attributes, such as hidden/unhidden status or style, change. If you call this method within an animation block, the changes are animated along with the rest of the animation block.

2. Implement [preferredStatusBarStyle][2] returning your preferred type.

It worked for me in iOS 10.1.

**Objective C**

[self setNeedsStatusBarAppearanceUpdate];

-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}

**Swift**

setNeedsStatusBarAppearanceUpdate()

var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

I am surprised nobody pointed this out. Anyway enjoy :)


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#30
Everything is much easier in Swift 3.0 Xcode 8

Using the code below in App Delegate file, after

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

insert this:

UINavigationBar.appearance().barStyle = .black

UINavigationBar.appearance().barTintColor = UIColor(red: 230, green: 32, blue: 31, alpha: 1.0)

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through