gpt4 book ai didi

ios - 如何更改 UISearchController 中 UISearchBar 的颜色

转载 作者:行者123 更新时间:2023-11-30 13:23:56 25 4
gpt4 key购买 nike

我有一个 UISearchBar,它是 UISearchController 的一部分,位于 UICollectionViewController 之上。我想更改 UISearchBar 的背景,使其成为我选择的图像,并且文本字段的背景为半透明。基本上,我想改变这个:

This is what it looks like with the given code

进入此:

After

我已经尝试了这里能找到的一切。我尝试了 searchBar.SetBackgroundImage 与 UISearchBarPosition 和 UIBarMetrics 的每一个可能的组合,但它们都不起作用。我所能达到的最好效果就是更改搜索图标。这对我没有任何帮助。我认为这是因为它嵌入在 UINavigationItem 中。我不知道为什么它不起作用。这是我设置的。

var resultSearchController:UISearchController? = nil

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let searchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTable") as! SearchTable
resultSearchController = UISearchController(searchResultsController: searchTable)
resultSearchController?.searchResultsUpdater = searchTable

//Sets up the search bar and adds it to navigation header
let searchBar = resultSearchController!.searchBar
searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchBar.sizeToFit()
searchBar.placeholder = "Find a Result"
navigationItem.titleView = resultSearchController?.searchBar
searchBar.setBackgroundImage(UIImage(named: "Horses.jpg"), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)

resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true


}

最佳答案

这是代码:

searchBar.setImage(UIImage(named: "Horses"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal);

var searchTextField: UITextField? = searchBar.valueForKey("searchField") as? UITextField
if searchTextField!.respondsToSelector(Selector("attributedPlaceholder")) {
var color = UIColor.purpleColor()
let attributeDict = [NSForegroundColorAttributeName: UIColor.purpleColor()]
searchTextField!.attributedPlaceholder = NSAttributedString(string: "search", attributes: attributeDict)
}

关于ios - 如何更改 UISearchController 中 UISearchBar 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37442618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com