向其中添加UISearchController时,导航栏变为白色 [英] Navigation bar becomes white when a UISearchController is added to it

查看:193
本文介绍了向其中添加UISearchController时,导航栏变为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从UINavigationController向UINavigationItem添加UISearchController时;当视图加载时,该图标变为白色;当用户单击搜索栏时,该图标变为指定的颜色.自iOS 13.1起发生这种情况.该视频显示了该行为:

When I add a UISearchController to a UINavigationItem from an UINavigationController; it becomes white when the view loads and changes to the color specified when the user clicks on the search bar. This happened since ios 13.1. This video shows the behaviour:

https://imgur.com/wn5zbnJ

我的代码由一个带有Storyboard的简单情节提要组成,其中包含NavigationController +一个TableViewController,NavigationController具有为其分配的颜色:

My code consists of a simple storyboard with a NavigationController + a TableViewController, and the NavigationController has a color assigned to it:

ViewController由以下代码组成:

The ViewController consists of the following code:

class ViewController: UITableViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = false
        navigationItem.searchController = searchController
    }
}

我还将这些键添加到了info.plist文件中,以强制应用进入灯光模式,但是如果我删除了这些键,仍然存在相同的行为:

I also added these keys to the info.plist file to force the app into light-mode, but if I remove these the same behaviour is still present:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

这已在运行iOS 13.1 beta 1的iPhone XS Max上进行了测试.这是预期的行为还是需要修复的错误?

This was tested on an iPhone XS Max, running iOS 13.1 beta 1. Is this expected behaviour or a bug which needs to be fixed?

推荐答案

似乎需要在iOS 13上使用新的UINavigationBarAppearance.尝试将其添加到您的viewDidLoad中:

It looks like it is required to use the new UINavigationBarAppearance on iOS 13. Try to add this to your viewDidLoad:

let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemRed
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

您可能还需要设置searchField backgroundColor:

You will probably also want to set the searchField backgroundColor:

let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground

这篇关于向其中添加UISearchController时,导航栏变为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆