Ios 导航栏背景图像未扩展到搜索栏
所以我以前把我的搜索控制器嵌入到导航栏中,这样导航栏的梯度也覆盖了搜索栏 突然,我相信在一次XCode更新中,我发现现在导航栏的背景色牢牢地停在搜索控制器的顶部。我很难确定为什么会发生这种情况 这是我当前的代码,以前是有效的。有什么想法吗Ios 导航栏背景图像未扩展到搜索栏,ios,swift,Ios,Swift,所以我以前把我的搜索控制器嵌入到导航栏中,这样导航栏的梯度也覆盖了搜索栏 突然,我相信在一次XCode更新中,我发现现在导航栏的背景色牢牢地停在搜索控制器的顶部。我很难确定为什么会发生这种情况 这是我当前的代码,以前是有效的。有什么想法吗 private func setNavigationBarAesthetics() { if let navigationBar = self.navigationController?.navigationBar { naviga
private func setNavigationBarAesthetics() {
if let navigationBar = self.navigationController?.navigationBar {
navigationBar.setBackgroundImage(UIImage(named: "testbg"), for: .default)
}
self.searchController.searchBar.delegate = self
self.searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.definesPresentationContext = false
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
searchController.hidesNavigationBarDuringPresentation = false
}
该代码有两个问题。首先,停止入侵搜索栏:
self.searchController.searchBar.barTintColor = UIColor.clear // no
self.searchController.searchBar.backgroundImage = UIImage() // no
searchController.searchBar.backgroundColor = UIColor.clear // no
searchController.searchBar.barStyle = .blackTranslucent // no
第二,停止攻击导航栏:完全从代码中删除GradientView
class GradientView: UIView { // no
只需使用框架提供的工具即可。设置导航栏的背景图像(为渐变图像)并停止。搜索控制器的搜索栏将自动正确集成到导航栏中:
编辑好的,所以这只是iOS 13的问题。这是因为你所做的不是如何在iOS 13中向导航栏添加背景图像。您必须将代码分成两部分:
if #available(iOS 13.0, *) {
let app = UINavigationBarAppearance()
app.backgroundImage = im // the gradient image
self.navigationController?.navigationBar.scrollEdgeAppearance = app
self.navigationController?.navigationBar.standardAppearance = app
} else {
// your old code goes here
}
处理ios 12和ios 13的Swift 5。 这是基于马特的回答
if #available(iOS 13.0, *) {
let app = UINavigationBarAppearance()
app.backgroundImage = UIImage(named: "img")
self.navigationController?.navigationBar.scrollEdgeAppearance = app
self.navigationController?.navigationBar.standardAppearance = app
} else {
// iOS 12 to below
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor(patternImage: UIImage(named: "img")!)
navigationbar.isTranslucent = false
}
}
“它突然改变了”这是什么意思?我怀疑XCode更新。好吧,但你没有解释问题是什么。说“改变”或“指出问题”并不能告诉我们问题是什么。哦,我需要蓝色渐变来覆盖整个搜索栏。我要澄清一下,抱歉,梯度是从哪里来的?给我们足够的代码,让我们自己重现这个问题。我遵循了你的帖子,它简化了我的编码。但这不会改变结果。那张图片的代码是什么样子的?嗨@markocalvocruz我告诉过你我在做什么。我将导航栏的背景图像设置为渐变图像,并将导航项的
searchController
设置为我的UISearchController。仅此而已(除了使圆形矩形背景为白色)。没有诡计。VC 100%嵌入在故事板的导航控制器中。根据你的回答,我刚刚更新了我的问题,以反映所有的代码更改。所以,你要做的不是如何在iOS 13中设置背景图像。您需要使用available
块来分割代码。我将发布ios13代码来设置背景图像。很好。但这个问题是关于iOS 13的。我回答了。结束。如果您现在有不同的问题,请提出新问题。