2015-12-15 37 views
2

如何使用SegmentedControlUISearchController?當SearchBar處於非活動狀態時,我需要SegmentedControl處於SearchBar之下,並在SearchBar被激活時隱藏分段控制。在UISearchController中使用UISegmentedControl

我把SearchController.searchBar放在桌面標題中,所以我在故事板中定義的SegmentedControlSearchBar重疊。當我試圖通過調用tableView.tableHeaderView?.addSubview(customSecmentedControl)以編程方式在viewDidLoad中添加SegmentedControl時,它重疊了tableView中的單元格。

回答

5

您無需連接自己的分段控件來處理此問題,因爲該功能已由UISearchController提供。

使用搜索欄的scopeButtonTitles屬性設置爲標題的範圍吧:

searchController.searchBar.scopeButtonTitles = ["Small", "Medium", "Large"] 

您可以通過訪問搜索欄的selectedScopeButtonIndex屬性來確定所選擇的範圍。

搜索欄發送其委託通知時的範圍變化:UISearchBarUISearchBarDelegate文檔中提供

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) 

進一步瞭解範圍欄的信息。

+0

嗨!非常感謝您的回答。我嘗試使用scopeBar,如下所述:http://www.raywenderlich.com/113772/uisearchcontroller-tutorial。 但據我所知,scopeBar僅在'SearchBar'處於活動狀態時出現。我需要在搜索處於非活動狀態時顯示分段控件,並在用戶在tableView中搜索某些內容時隱藏它。 –

+0

你必須自己隱藏和顯示它,但['UISearchControllerDelegate'](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchControllerDelegate_Ref/)中的方法應該是有用的完成這一點。 –

+0

@FilippIgnatov另一種方法是在導航欄'titleView'中放置一個分段控件。由於searchBar覆蓋導航欄,因此搜索時會隱藏您的控件。 –

相關問題