簡體   English   中英

如何在UISearchController中更改UISearchBar的顏色

[英]How to change the color of a UISearchBar in a UISearchController

我有一個UISearchBar,它是UISearchController的一部分,位於UICollectionViewController的頂部。 我想更改UISearchBar的背景,以便它是我選擇的圖像,並且文本字段的背景是半透明的。 基本上,我想把這個變成:

這就是給定代碼的樣子

變成這個:

后

我已經盡力在這里找到所有東西。 我嘗試將SearchSearch.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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM