2016-05-25 3 views
0

UISearchControllerの一部であるUISearchBarがあります。これはUICollectionViewControllerの上にあります。私はUISearchBarのバックグラウンドを変更して、自分が選んだイメージで、テキストフィールドの背景を半透明にしたいと思っています。基本的に、私はこれをオンにする:この中へUISearchControllerで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 


    } 

答えて

0

ここでは、コードです:

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) 
} 
+0

いいえ。アイコンが変更されます。私は背景イメージを変更したい – QuantumHoneybees

関連する問題