【问题标题】:Changing MFMailComposeViewControllers status bar style更改 MFMailComposeViewControllers 状态栏样式
【发布时间】:2017-12-13 23:06:36
【问题描述】:

我无法让状态栏以白色显示时钟电池等。 我读了很多关于堆栈溢出的类似问题,但大多数都是旧的并且不是用 swift 编写的。我能找到的最新答案建议override func preferredStatusBarStyle 不再是一个函数。我尝试了以下方法,但它不起作用。

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()
    }
    
}

我调用视图控制器使用。

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)

视图控制器缺少什么来更改其StatusBarStyle

【问题讨论】:

  • Swift 2 - UIApplication.sharedApplication().statusBarStyle = .LightContent Swift 3 - UIApplication.shared.statusBarStyle = .lightContent
  • 您是否将这个“基于控制器的状态栏外观”添加到应用信息列表中
  • @zombie 是的,我做到了
  • @SagarSnehi 我不太清楚你的评论是什么意思。我已经在我的AppDelegate 中设置了UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

标签: ios swift


【解决方案1】:

首先在应用信息列表中添加键“View controller-based status bar appearance”,值为NO

然后检查此代码

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
   }
}

【讨论】:

  • 将样式移至viewDidAppear 有效。对于将来遇到此问题的任何其他人。如果您不想在关闭邮件发送视图后将样式更改为黑色,只需从上面的代码中省略整个 viewDidDisappear 函数即可。
猜你喜欢
  • 2016-01-11
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多