Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 可见UISearchBar更改UINavigationBar背景色_Ios_Swift_Uinavigationbar_Uisearchbar_Ios12 - Fatal编程技术网

Ios 可见UISearchBar更改UINavigationBar背景色

Ios 可见UISearchBar更改UINavigationBar背景色,ios,swift,uinavigationbar,uisearchbar,ios12,Ios,Swift,Uinavigationbar,Uisearchbar,Ios12,tableview控制器嵌入到导航控制器中。 我以编程方式向tableview控制器的导航栏添加了一个搜索栏。我只将导航栏背景颜色更改为与默认颜色不同的颜色(紫色)-其余的我都保留了默认颜色 class TableViewController: UITableViewController { let searchController = UISearchController(searchResultsController: nil) override func viewDidLo

tableview控制器嵌入到导航控制器中。 我以编程方式向tableview控制器的导航栏添加了一个搜索栏。我只将导航栏
背景
颜色更改为与默认颜色不同的颜色(紫色)-其余的我都保留了默认颜色

class TableViewController: UITableViewController {
    let searchController = UISearchController(searchResultsController: nil)
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.hidesSearchBarWhenScrolling = true
        navigationItem.searchController = searchController
    }
}
出于演示目的,上述代码减少到最低限度。
所有这些都是使用Xcode 11(11A420a)完成的

我在iOS 12.0和13.0模拟器和设备中运行该项目

  • iOS 13.0
    搜索栏在开始时显示。
    导航栏背景色显示正确。
    滚动时,导航栏背景颜色保持正确
有了iOS 13.0,一切正常

  • iOS 12.0
    搜索栏在启动时不显示。
    导航栏背景色显示正确。
    滚动时,只要搜索栏可见,导航栏的背景色就会变为白色
我试图以编程方式更改故事板中的所有颜色设置以及属性。当搜索栏可见时,我没有成功更改导航栏的背景色

当搜索栏变得可见时,导航栏前景似乎失去了透明度

如果我使用导航栏的
条色调
颜色(!=
默认值
),所有功能都会按预期工作(与iOS 13.0一样),但我会释放渐变效果,我希望保留渐变效果

我错过了什么?
如何避免这种情况?
臭虫


遇到类似问题时,我很幸运地使用了
navigationItem.scrollEdgeAppearance
属性。例如:

vc.navigationItem.scrollEdgeAppearance?.backgroundColor = .red
但这仅在iOS 13上可用

下面是我在允许搜索控制器的滚动条在滚动过程中隐藏的同时,在导航栏中获得正确颜色的最终操作:

if #available(iOS 13.0, *) {

    let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)
    navbar.standardAppearance.backgroundEffect = blurEffect
    navbar.standardAppearance.backgroundColor = appMainColor.withAlphaComponent(0.75)
    navbar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    navbar.compactAppearance = nil
    navbar.scrollEdgeAppearance = navbar.standardAppearance.copy()

    navitem.standardAppearance = nil
    navitem.compactAppearance = nil
    navitem.scrollEdgeAppearance = nil
}

我不知道这是否是你想要的外观,但我发现如果你在滚动背景停止改变颜色时禁用隐藏搜索栏。但是,搜索栏总是在那里

将此添加到viewDidLoad():

navigationItem.hidesSearchBarWhenScrolling = false