首页 > 解决方案 > 在 iOS 13 中使用 UISearchController 时,状态栏在 iOS 中变为白色

问题描述

这里有一个奇怪的情况。当我使用 时UISearchController,我首先得到这个外观(如预期的那样)

在此处输入图像描述

但是,当您在 TextField 内选择开始搜索时,状态栏将变为完全白色(如果您处于暗模式,则为黑色)

在此处输入图像描述

这从来没有发生过。是否有一些设置UISearchController告诉它在使用搜索栏时使用某种状态栏样式?

我希望它保持选择 TextField 之前的颜色

- -编辑 - -

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)

    frc = getFRC()
    frc.delegate = self

    self.resultsSearchController.delegate = self
    let searchBar = self.resultsSearchController.searchBar

    self.resultsSearchController.searchResultsUpdater = self
    self.resultsSearchController.obscuresBackgroundDuringPresentation = false
    self.resultsSearchController.extendedLayoutIncludesOpaqueBars = true

    searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchBar
    searchBar.placeholder = "Catalog Search"
    searchBar.barTintColor = UIColor.darkAqua
    searchBar.searchTextField.backgroundColor = UIColor.white

    self.definesPresentationContext = true

    searchBarHeight = searchBar.frame.height

    do {
        try frc.performFetch()
    } catch {
        error.tryError(tryMessage: "Perform initial fetch", loc: self)
    }

    if tutorials.catalog {
        createTutorialTab(segueNameOnOpen: "catalogTutorial")
    }
}

标签: iosswiftuisearchcontroller

解决方案


您可以使用此功能更改状态栏颜色,这是处理状态栏的一种技巧;)。如果您在整个应用程序中使用一个主题状态栏,请从 AppDelegate 中的 didFinishLaunching 调用此函数。

func changeStatusBar(backgroundColor: UIColor, contentColor:UIColor) {

    if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
        statusBar.backgroundColor = backgroundColor
        statusBar.setValue(contentColor, forKey: "foregroundColor")
    }
}

推荐阅读