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

#11
**In Swift 4 or 4.2**

You can add on your vc

> preferredStatusBarStyle

and set return value to

> .lightContent or .default

ex:

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Reply

#12
Another really easy way to make this work is just to create an extension of the UINavigationController class.

Since overriding the `preferredStatusBarStyle:` method wont work **UNLESS** we do it inside of the UINavigationController class.

extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

Reply

#13
***WARNING***


----------
Setter for 'statusBarStyle' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]

UIApplication.shared.statusBarStyle = .default

----------
so my solution was as this:
making an extension from the navigation controller:

extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if let topViewController = presentedViewController{
return topViewController.preferredStatusBarStyle
}
if let topViewController = viewControllers.last {
return topViewController.preferredStatusBarStyle
}

return .default
}
}

and if you have a viewController that will have another style than the style of the app , you can make this

var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
return barStyle
}
lets say that you app status style is `.default` and you want this screen to be `.lightContent`
so barStyle will take the `.lightContent` as its default value, this will change the status bar style to lightContent, and then make sure when `viewWillDisappear` change the barStyle again to the app status bar style which in our case is `.default` .

***this is works for me***
Reply

#14
Works for Navigation Based for particular view controller in swift4

let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height

let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
Reply

#15
**SWIFT 4.2**
Hey, I wanted to share a solution, that worked for me that I got from a great article on this ellusive subject by Graig Grummitt.

Step 1
As others have mentioned ADD below to your PLIST

View controller-based status bar appearance YES

Step 2 in the RootViewcontroller add below

var statusBarHidden: Bool = false {
didSet(newValue) {
UIView.animate(withDuration: 0.1) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}

override var prefersStatusBarHidden: Bool {
return statusBarHidden
}

var vcStatusBarStyle: UIStatusBarStyle = .default {
didSet(newValue) {
UIView.animate(withDuration: 0.1) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return vcStatusbarStyle
}

When updating either property `statusBarHidden` or `vcStatusBarStyle` it will call `setNeedsStatusBarAppearanceUpdate()` and will update the status bar with the new values for either `prefersStatusBarHidden` or `preferredStatusBarStyle`. In my situation I had to update these properties for the container viewcontroller, that was the parent of the visable childviewcontroller. I did this using a simple delegate method.

protocol MainViewControllerDelegate {
func updateStatusBarStyle(statBarStayle: UIStatusBarStyle)
func toggleStatusBar(visable: Bool)
}

Ofcourse when instantiating the childViewController(Visible VC) don't forget to set the MainViewcontroller(Container VC) as its delegate. I sometimes do. :)

childViewController.delegate = self

Then in the childViewController I just called the delegate method when needed to update the status bar.

self.delegate?.updateStatusBarStyle(statBarStayle: .default)

As mentioned above Graig Grummitt goes into more detail about this solution and also working with UINavigationControllers as well. Link here: [The Mysterious Case of the Status Bar][1]


[1]:

[To see links please register here]

Reply

#16
**Swift 4.2 solution with NavigationController**

**First Step:**

Open your info.plist and insert a new key named "*View controller-based status bar appearance*" or `UIViewControllerBasedStatusBarAppearance` to *YES* to let each VC use their own status property.

**Second Step**

In each VC, override the *preferredStatusBarStyle* property like this :

override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent //.default for black style
}

**Last step**

Override the *preferredStatusBarStyle* property in your custom NavigationController class :

class NavigationController : UINavigationController {

override var preferredStatusBarStyle : UIStatusBarStyle {

if let topVC = viewControllers.last {
//return the status property of each VC, look at step 2
return topVC.preferredStatusBarStyle
}

return .default
}
Reply

#17
For Xcode 10 you can create a class and put it before your viewController class, you can call this class in all view controller is needed a light content status bar...

class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
Now change your viewController class in:

class YourViewController: UIViewControllerWithLightStatusBar {
...
}
And that's all...
Reply

#18
**Swift 4** For specific ViewController without navigationViewController embedded just add this to your ViewController file.

override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
Reply

#19
I followed this tutorial and it worked for me. However, I am not sure if there are any caveats.

[To see links please register here]


- Open your info.plist and set
`UIViewControllerBasedStatusBarAppearance` to `false`.
- In the first function in `AppDelegate.swift`, which contains `didFinishLaunchingWithOptions`, set the color you want.

`UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent`

* **Swift 3 Update** *

UIApplication.shared.statusBarStyle = .lightContent
Reply

#20
What worked with me, in the Storyboard, go to the Navigation Controller, select the navigation bar, click on the Attributes Inspector, then change the style from default to black. That's it!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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