在 iOS11 中更改 UISearchBar 背景图片 [英] Change UISearchBar background image in iOS11

查看:16
本文介绍了在 iOS11 中更改 UISearchBar 背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改我的 UISearchBar 背景图片.在我的 UITableView 中将它添加为标题视图时,它工作得非常好.但是,当我想将 SearchBar 更改为设置为 navigationItem 的 searchController 时,它不会更改背景图像.

I want to change my UISearchBar background image. When adding it as the header view in my UITableView it works perfectly fine. However, when I want to change it for the SearchBar being set as the searchController of the navigationItem it won´t change the background image.

我正在使用:

self.searchBar.searchBar.setBackgroundImage(UIImage(named: "#IMAGENAME")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .any, barMetrics: UIBarMetrics.default)

有人知道如何解决这个问题吗?

Does anybody know how to fix this issue?

推荐答案

您可能正在寻找这个:

if #available(iOS 11.0, *) {
    let sc = UISearchController(searchResultsController: nil)
    if let navigationbar = self.navigationController?.navigationBar {
        navigationbar.barTintColor = UIColor(patternImage: UIImage(named: "pattern")!)
    }
    navigationItem.searchController = sc
    navigationItem.hidesSearchBarWhenScrolling = false
}

这是图片:

这是结果:

如果您想更改搜索栏的任何其他 UI 元素,这里有更详细的答案.

Here is more detailed answer, if you want to change any other UI elements of search bar.

这篇关于在 iOS11 中更改 UISearchBar 背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆