首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS 11 UISearchBar in UINavigationBar

iOS 11 UISearchBar in UINavigationBar
EN

Stack Overflow用户
提问于 2017-09-01 10:14:13
回答 7查看 17.9K关注 0票数 25

我想把一个搜索栏在新的导航栏与新的iOS 11大标题。但是,搜索栏的颜色是由iOS自动应用的,我无法更改它。

代码语言:javascript
运行
复制
if #available(iOS 11.0, *) {
    let sc = UISearchController(searchResultsController: nil)
    navigationItem.searchController = sc
    navigationItem.hidesSearchBarWhenScrolling = false
}

搜索栏是深蓝色背景,但我只想把它改成白色。

设置背景色的结果如下:

代码语言:javascript
运行
复制
navigationItem.searchController?.searchBar.backgroundColor = UIColor.white

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

此外,当我通过点击搜索栏触发搜索时,它切换到右侧的cancel按钮的模式。(德语中的“Abbrechen”)

而"Abbrechen“文本的颜色也不能改变。(也需要白色)

任何帮助都是非常感谢的。

编辑:根据请求,在这里显示导航条样式的代码:

代码语言:javascript
运行
复制
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()]
}

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

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

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2017-09-01 12:06:44

这就是你想要的..。

代码语言:javascript
运行
复制
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

}

结果:

圆角

带有圆角的动画也工作得很好。

票数 21
EN

Stack Overflow用户

发布于 2017-10-08 11:57:17

我在AppDelegate中用这段代码更改了文本字段的背景。

Swift 4

代码语言:javascript
运行
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //background color of text field
         UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
        
        }

这就是结果

票数 4
EN

Stack Overflow用户

发布于 2017-09-01 10:50:43

这应该对你有用

代码语言:javascript
运行
复制
func addSearchbar(){
        if #available(iOS 11.0, *) {
            let sc = UISearchController(searchResultsController: nil)
            let scb = sc.searchBar
            scb.tintColor = UIColor.white

            if let navigationbar = self.navigationController?.navigationBar {
                //navigationbar.tintColor = UIColor.green
                //navigationbar.backgroundColor = UIColor.yellow
                navigationbar.barTintColor = UIColor.blue
            }

            navigationController?.navigationBar.tintColor = UIColor.green
            navigationItem.searchController = sc
            navigationItem.hidesSearchBarWhenScrolling = false
        }
}

结果

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45997996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档