UINavigationBar中的iOS 11 UISearchBar

Dar*_*rko 21 uinavigationbar uisearchbar ios swift ios11

我想在新的导航栏中放置一个搜索栏,其中包含新的iOS 11大标题.但是,搜索栏的颜色会自动由iOS应用,我无法更改它.

if #available(iOS 11.0, *) {
    let sc = UISearchController(searchResultsController: nil)
    navigationItem.searchController = sc
    navigationItem.hidesSearchBarWhenScrolling = false
}
Run Code Online (Sandbox Code Playgroud)

搜索栏是深蓝色背景,但我只想将其更改为白色.

在此输入图像描述

设置背景颜色会导致:

navigationItem.searchController?.searchBar.backgroundColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我也试过setScopeBarButtonBackgroundImagesetBackgroundImage在搜索栏上但是一切看起来都很奇怪.

此外,当我通过点击搜索栏触发搜索时,它会切换到右侧有取消按钮的模式.("Abbrechen"德文)

在此输入图像描述

并且"Abbrechen"文本颜色也无法更改.(需要它也是白色的)

任何帮助表示赞赏.

编辑:根据要求,这里是导航栏样式的代码:

self.navigationBar.tintColor = UIColor.myWhite
self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.myWhite, NSAttributedStringKey.font: UIFont.myNavigationBarTitle()]
self.navigationBar.barTintColor = UIColor.myTint

if #available(iOS 11.0, *) {
    self.navigationBar.prefersLargeTitles = true
    self.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.myWhite, NSAttributedStringKey.font: UIFont.myNavigationBarLargeTitle()]
}
Run Code Online (Sandbox Code Playgroud)

目前的结果:我使用Krunals的想法来设置搜索栏背景的颜色,但圆角丢失了.重新设置圆角后,搜索栏的动画似乎被打破.

在此输入图像描述

所以仍然没有令人满意的解 似乎嵌入到iOS 11导航栏中的搜索栏无法自定义.同时,仅仅改变占位符文本的颜色就足够了,但即使这样也不可能.(我尝试过StackOverflow的多种方法 - 不起作用)

Kru*_*nal 19

现在这就是你想要的......

if #available(iOS 11.0, *) {
            let sc = UISearchController(searchResultsController: nil)
            sc.delegate = self
            let scb = sc.searchBar
            scb.tintColor = UIColor.white
            scb.barTintColor = UIColor.white


            if let textfield = scb.value(forKey: "searchField") as? UITextField {
                textfield.textColor = UIColor.blue
                if let backgroundview = textfield.subviews.first {

                    // Background color
                    backgroundview.backgroundColor = UIColor.white

                    // Rounded corner
                    backgroundview.layer.cornerRadius = 10;
                    backgroundview.clipsToBounds = true;

                }
            }

            if let navigationbar = self.navigationController?.navigationBar {
                navigationbar.barTintColor = UIColor.blue
            }
            navigationItem.searchController = sc
            navigationItem.hidesSearchBarWhenScrolling = false

}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

在此输入图像描述


圆角:
带圆角的动画也很好用.

在此输入图像描述