添加 UISearchController 后,导航栏变为白色
Navigation bar becomes white when a UISearchController is added to it
当我将 UISearchController 添加到 UINavigationController 中的 UINavigationItem 时;当视图加载并更改为用户单击搜索栏时指定的颜色时,它变为白色。这是自 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
文件中以强制应用程序进入轻模式,但如果我删除这些密钥,相同的行为仍然存在:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
这是在 iPhone XS Max 运行 iOS 13.1 beta 1 上测试的。这是预期的行为还是需要修复的错误?
你的问题没有明确说明你想要什么。但是,如果您愿意,将搜索栏添加到导航栏中并使用特定颜色,这可能对您有所帮助。
将seachbar放入导航栏的过程:
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
您可以将您想要的任何控制器添加到 'searchResultsController' 值中。
如果要将背景颜色设置为特定的颜色,可以从 Storyborad -> navigationbar -> navigation bar attiribute inspection 更改 bar tint。
此外,AppDelegate.swift 文件的以下代码也将执行相同的操作。 'tintcolor' 和 'titletextcolor' 被评论
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = UIColor.blue
// navigationBarAppearace.tintColor = UIColor.red
//navigationBarAppearace.titleTextAttributes = [.foregroundColor: UIColor.red]
return true
}
我遇到了同样的问题,并且已经打开了 Apple 的雷达。
即使,如果您将 backgroundColor 设置为 UINavigationBar 的外观,那么当下拉并显示搜索栏时,导航栏不会变得透明,而是变成该颜色,但状态栏仍然保持白色。
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
UINavigationBar.appearance().backgroundColor = .red
这里有一些代码试图减轻这种行为,但我很乐意听到状态栏的事情
这似乎是 iOS 13.1 中的错误。具体来说,导航栏有一个新的 iOS 13 特定外观 (UINavigationBarAppearance),它指定滚动视图滚动到顶部时的外观,以及默认状态。通常这样的更改只有在使用相应的 SDK (iOS 13.1) 构建应用程序时才会生效。但是,似乎存在一个错误,即当使用 iOS 12 SDK 构建应用程序时也会出现该行为。
参见:https://developer.apple.com/documentation/uikit/uinavigationbarappearance
更新:
这里有一个解决方法:https://itnext.io/fixing-issues-caused-by-future-sdks-ae0896384abf
本质上,如果您的应用程序 运行 在设备 运行 iOS 13 上,则可以通过 NSClassFromString() 在 swift,然后使用一点 objective-c 运行时魔法来配置导航栏。
看起来需要在 iOS 上使用新的 UINavigationBarAppearance 13。尝试将其添加到您的 viewDidLoad:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemRed
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
您可能还想设置搜索字段背景颜色:
let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground
当我将 UISearchController 添加到 UINavigationController 中的 UINavigationItem 时;当视图加载并更改为用户单击搜索栏时指定的颜色时,它变为白色。这是自 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
文件中以强制应用程序进入轻模式,但如果我删除这些密钥,相同的行为仍然存在:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
这是在 iPhone XS Max 运行 iOS 13.1 beta 1 上测试的。这是预期的行为还是需要修复的错误?
你的问题没有明确说明你想要什么。但是,如果您愿意,将搜索栏添加到导航栏中并使用特定颜色,这可能对您有所帮助。
将seachbar放入导航栏的过程:
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
您可以将您想要的任何控制器添加到 'searchResultsController' 值中。
如果要将背景颜色设置为特定的颜色,可以从 Storyborad -> navigationbar -> navigation bar attiribute inspection 更改 bar tint。
此外,AppDelegate.swift 文件的以下代码也将执行相同的操作。 'tintcolor' 和 'titletextcolor' 被评论
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = UIColor.blue
// navigationBarAppearace.tintColor = UIColor.red
//navigationBarAppearace.titleTextAttributes = [.foregroundColor: UIColor.red]
return true
}
我遇到了同样的问题,并且已经打开了 Apple 的雷达。 即使,如果您将 backgroundColor 设置为 UINavigationBar 的外观,那么当下拉并显示搜索栏时,导航栏不会变得透明,而是变成该颜色,但状态栏仍然保持白色。
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
UINavigationBar.appearance().backgroundColor = .red
这里有一些代码试图减轻这种行为,但我很乐意听到状态栏的事情
这似乎是 iOS 13.1 中的错误。具体来说,导航栏有一个新的 iOS 13 特定外观 (UINavigationBarAppearance),它指定滚动视图滚动到顶部时的外观,以及默认状态。通常这样的更改只有在使用相应的 SDK (iOS 13.1) 构建应用程序时才会生效。但是,似乎存在一个错误,即当使用 iOS 12 SDK 构建应用程序时也会出现该行为。
参见:https://developer.apple.com/documentation/uikit/uinavigationbarappearance
更新: 这里有一个解决方法:https://itnext.io/fixing-issues-caused-by-future-sdks-ae0896384abf
本质上,如果您的应用程序 运行 在设备 运行 iOS 13 上,则可以通过 NSClassFromString() 在 swift,然后使用一点 objective-c 运行时魔法来配置导航栏。
看起来需要在 iOS 上使用新的 UINavigationBarAppearance 13。尝试将其添加到您的 viewDidLoad:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemRed
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
您可能还想设置搜索字段背景颜色:
let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground