为什么 UISearchController 会更改导航栏颜色?
Why is UISearchController changing the navigation bar colors?
我已经使用 Xcode 11 (iOS 13) 在故事板中定义了 2 个视图控制器的示例项目上对此进行了测试。 "presenting" 视图控制器嵌入在导航控制器中,并在 viewWillAppear
中设置了导航栏颜色。
"search" 视图控制器在 viewDidLoad
中添加了一个 UISearchController
并由呈现视图控制器(非模态)推送。
仅通过此设置,当显示搜索视图控制器时,导航栏就具有预期的蓝色背景和红色色调。但是,当向下滚动并显示搜索栏时,导航栏的背景颜色会丢失(或更改为默认的 iOS 灰色/半透明)。但是,如果您向上滚动(隐藏搜索栏)或关注搜索栏文本字段,导航栏颜色 returns!
此外,如果您关注搜索栏文本字段然后取消(通过点击“取消”按钮),导航栏的色调颜色将从红色恢复为默认的 iOS 蓝色,可以注意到返回栏项目。
对解决这个问题有什么建议吗?
我也在搜索控制器的 viewWillAppear
中设置了导航栏颜色,这并没有改变这种行为。
我将搜索控制器中导航栏的 isTranslucent
设置为 true
,这似乎阻止了背景颜色的恢复,但它并没有改变取消时色调颜色的恢复。
呈现视图控制器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .red
}
搜索视图控制器
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Search VC"
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true
}
编辑
按照建议设置 scrollEdgeAppearance
、backButtonAppearance
和 buttonAppearance
是一种享受,但默认为 iOS 蓝色的系统栏按钮除外。这可以通过设置 UINavigationBar.tintColor 来解决,但都不能解决取消搜索时后退按钮 V 形颜色默认设置的问题。
if #available(iOS 13.0, *) {
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.configureWithDefault(for: .plain)
buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.red]
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .blue
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.red]
navigationBarAppearance.backButtonAppearance = buttonAppearance
navigationBarAppearance.buttonAppearance = buttonAppearance
navigationBarAppearance.doneButtonAppearance = buttonAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
} else {
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.red]
navigationController?.navigationBar.tintColor = .red
}
However when scrolling down and the search bar is displayed the background color of the navigation bar is lost
这一切都是正常的。 iOS 13 中的新功能,扩展的导航栏(显示搜索栏、大标题等)与普通导航栏的外观不同。您的设置仅适用于普通导航栏,因为您没有将它们设为 iOS 13 方式。如果您希望展开的导航栏看起来像普通导航栏,您必须单独明确地设置它的外观。
为此,您需要设置导航栏的 scrollEdgeAppearance
。研究 类 UIBarAppearance、UINavigationBarAppearance 和 UIBarButtonItemAppearance(您需要明确设置 backButtonAppearance
)。
我已经使用 Xcode 11 (iOS 13) 在故事板中定义了 2 个视图控制器的示例项目上对此进行了测试。 "presenting" 视图控制器嵌入在导航控制器中,并在 viewWillAppear
中设置了导航栏颜色。
"search" 视图控制器在 viewDidLoad
中添加了一个 UISearchController
并由呈现视图控制器(非模态)推送。
仅通过此设置,当显示搜索视图控制器时,导航栏就具有预期的蓝色背景和红色色调。但是,当向下滚动并显示搜索栏时,导航栏的背景颜色会丢失(或更改为默认的 iOS 灰色/半透明)。但是,如果您向上滚动(隐藏搜索栏)或关注搜索栏文本字段,导航栏颜色 returns!
此外,如果您关注搜索栏文本字段然后取消(通过点击“取消”按钮),导航栏的色调颜色将从红色恢复为默认的 iOS 蓝色,可以注意到返回栏项目。
对解决这个问题有什么建议吗?
我也在搜索控制器的 viewWillAppear
中设置了导航栏颜色,这并没有改变这种行为。
我将搜索控制器中导航栏的 isTranslucent
设置为 true
,这似乎阻止了背景颜色的恢复,但它并没有改变取消时色调颜色的恢复。
呈现视图控制器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .red
}
搜索视图控制器
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Search VC"
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true
}
编辑
按照建议设置 scrollEdgeAppearance
、backButtonAppearance
和 buttonAppearance
是一种享受,但默认为 iOS 蓝色的系统栏按钮除外。这可以通过设置 UINavigationBar.tintColor 来解决,但都不能解决取消搜索时后退按钮 V 形颜色默认设置的问题。
if #available(iOS 13.0, *) {
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.configureWithDefault(for: .plain)
buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.red]
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .blue
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.red]
navigationBarAppearance.backButtonAppearance = buttonAppearance
navigationBarAppearance.buttonAppearance = buttonAppearance
navigationBarAppearance.doneButtonAppearance = buttonAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
} else {
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.red]
navigationController?.navigationBar.tintColor = .red
}
However when scrolling down and the search bar is displayed the background color of the navigation bar is lost
这一切都是正常的。 iOS 13 中的新功能,扩展的导航栏(显示搜索栏、大标题等)与普通导航栏的外观不同。您的设置仅适用于普通导航栏,因为您没有将它们设为 iOS 13 方式。如果您希望展开的导航栏看起来像普通导航栏,您必须单独明确地设置它的外观。
为此,您需要设置导航栏的 scrollEdgeAppearance
。研究 类 UIBarAppearance、UINavigationBarAppearance 和 UIBarButtonItemAppearance(您需要明确设置 backButtonAppearance
)。