2017-01-24 10 views
1

私はカスタマイズされたUISearchBarを持っています。ユーザがsearchBarをクリックすると、追加のボタンを表示したいと思います。だから、私は追加のボタンを表示するscopeButtonTitlesプロパティを使用しています。しかし、それはUISearchBarには表示されません。カスタマイズされたUISearchBarにscopeButtonTitlesを実装する方法は?

customSearchController = CustomSearchController(searchResultsController: self, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: tableView.frame.size.width, height: 50.0), searchBarFont: UIFont.systemFont(ofSize: 16.0),                searchBarTextColor: UIColor.white, searchBarTintColor: UIColor.primaryBlue()) 
customSearchController.hidesNavigationBarDuringPresentation = false 
customSearchController.dimsBackgroundDuringPresentation = false 
customSearchController.customSearchBar.placeholder = NSLocalizedString("Search", comment: "Search") 
customSearchController.customSearchBar.accessibilityLabel = customSearchController.searchBar.placeholder 
//Scope Buttons 
customSearchController.customSearchBar.scopeButtonTitles = [NSLocalizedString("List", comment: "List"), NSLocalizedString("Map", comment: "Map")] 

カスタマイズしたUISearchBarを使用していない場合は問題ありません。実際の問題は、UISearchBarをカスタマイズするときです。

答えて

1

showsScopeBarのプロパティをUISearchBarからtrueに設定すると、scopeBarが表示されます。

customSearchController.customSearchBar.showsScopeBar = true 

編集:あなたは、デリゲートメソッドsearchBarTextDidBeginEditingを使用してscopebarを表示することができますそのために。あなたが検索した後scoprBarを非表示にする場合

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 
    customSearchController.customSearchBar.showsScopeBar = true 
    //set the frame so it will show scope bar properly. 
} 

さて、あなたは、スコープバーを非表示にするsearchBarCancelButtonClickedを使用することができます。

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
    customSearchController.customSearchBar.showsScopeBar = false 
    //set the frame again 
} 
+0

あなたの答えをありがとう、私はあなたのアプローチを試みた、それは働いている。しかし、それはUISearchBarと重複しており、最初は現れています。実際には、ユーザーが検索ボタンをクリックした後にのみ、追加のボタンを表示したいと考えています。また、私はUISearchBar CGRectの価値をハードコーディングしています。 – PradeshV

+0

@PradeshV私の編集した答えを確認してください。 –

+0

それは動作します:)ありがとう。 – PradeshV

関連する問題