当我从UINavigationController向UINavigationItem添加UISearchController时;当视图加载时,该图标变为白色;当用户单击搜索栏时,该图标变为指定的颜色。自iOS 13.1起发生这种情况。该视频显示了该行为:
我的代码包含一个简单的情节提要,其中包含一个NavigationController+一个TableViewController,并且NavigationController为其分配了一种颜色:
ViewController由以下代码组成:
class ViewController: UITableViewController { let searchController = UISearchController(searchResultsController: nil) override func viewDidLoad() { super.viewDidLoad() searchController.hidesNavigationBarDuringPresentation = false searchController.obscuresBackgroundDuringPresentation = false navigationItem.searchController = searchController } }
我还将这些键添加到info.plist文件中,以强制应用程序进入灯光模式,但如果删除这些键,则仍然存在相同的行为:
info.plist
<key>UIUserInterfaceStyle</key> <string>Light</string>
这已在运行iOS 13.1 beta 1的iPhone XS Max上进行了测试。这是预期的行为还是需要修复的错误?
看来需要在iOS 13上使用新的UINavigationBarAppearance。尝试将其添加到您的viewDidLoad中:
let appearance = UINavigationBarAppearance() appearance.backgroundColor = .systemRed appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white] navigationItem.standardAppearance = appearance navigationItem.scrollEdgeAppearance = appearance
您可能还需要设置searchField backgroundColor:
let searchField = searchController.searchBar.searchTextField searchField.backgroundColor = .systemBackground