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(["[email protected]"])
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?

5
  • Swift 2 - UIApplication.sharedApplication().statusBarStyle = .LightContent Swift 3 - UIApplication.shared.statusBarStyle = .lightContent Commented Jul 10, 2017 at 9:35
  • did you add this "View controller-based status bar appearance" to the app info list Commented Jul 10, 2017 at 9:39
  • @zombie yes I did Commented Jul 10, 2017 at 9: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 Commented Jul 10, 2017 at 9:47
  • @lukasz check this link stackoverflow.com/questions/26956728/… Commented Jul 10, 2017 at 9:54

1 Answer 1

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(["[email protected]"])
   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
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.