1

I can't get the status bar to display the clock battery etc in white. I read a lot of similar questions on stack overflow but most are old and not written in swift. The most recent answers I could find suggested to override func preferredStatusBarStyle which is no longer a function. I tried the following, but it doesn't work.

import Foundation
import UIKit
import MessageUI

class ContactUsViewController: MFMailComposeViewController {
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        print("mail viewDidLoad()")
        self.setNeedsStatusBarAppearanceUpdate()
    }
    
}

I call the view controller using.

if !MFMailComposeViewController.canSendMail() {
    print("Mail services are not available")
    return
}
self.timeSlider.removeFromSuperview()
let contactVC = ContactUsViewController()// MFMailComposeViewController()
contactVC.navigationBar.tintColor = UIColor.white
contactVC.mailComposeDelegate = self
            
// Configure the fields of the interface.
contactVC.setToRecipients(["support@example.com"])
contactVC.setSubject("Your subject here")
contactVC.setMessageBody("Enter message about bugs, problems, ideas how to make the app better etc.", isHTML: false)
contactVC.modalPresentationCapturesStatusBarAppearance = true
// Present the view controller
self.navigationController?.present(contactVC, animated: true, completion: nil)

What is missing from the view controller to change its StatusBarStyle?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lukasz
  • 2,257
  • 3
  • 26
  • 44
  • Swift 2 - UIApplication.sharedApplication().statusBarStyle = .LightContent Swift 3 - UIApplication.shared.statusBarStyle = .lightContent – Sagar Snehi Jul 10 '17 at 09:35
  • did you add this "View controller-based status bar appearance" to the app info list – zombie Jul 10 '17 at 09:39
  • @zombie yes I did – Lukasz Jul 10 '17 at 09:40
  • @SagarSnehi I'm not quite sure what you mean with your comment. I already do set `UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent` inside my `AppDelegate` – Lukasz Jul 10 '17 at 09:47
  • @lukasz check this link https://stackoverflow.com/questions/26956728/changing-the-status-bar-color-for-specific-viewcontrollers-using-swift-in-ios8 – Sagar Snehi Jul 10 '17 at 09:54

1 Answers1

2

First in the app info list add the key "View controller-based status bar appearance" with the value of NO

then check this code

func showContactUs() {
   if !MFMailComposeViewController.canSendMail() {
      print("Mail services are not available")
      return
   }
   let contactVC = ContactUsViewController()

   //To make the nav bar stand out
   //contactVC.navigationBar.barStyle = .blackTranslucent

   contactVC.setToRecipients(["support@example.com"])
   contactVC.setSubject("Your subject here")
   contactVC.setMessageBody("Enter message about bugs, problems, ideas how to make the app better etc.", isHTML: false)
   present(contactVC, animated: true)
}


class ContactUsViewController: MFMailComposeViewController {

   override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)

      UIApplication.shared.statusBarStyle = .lightContent
   }

   override func viewDidDisappear(_ animated: Bool) {
      super.viewDidDisappear(animated)

      UIApplication.shared.statusBarStyle = .default
   }
}
zombie
  • 5,069
  • 3
  • 25
  • 54
  • Moving the styling to `viewDidAppear` works. For anyone else running into this problem in the future. If you don't want to change the style to black after closing the mail sending view, simply omit the entire `viewDidDisappear` function from the code above. – Lukasz Jul 10 '17 at 10:46