简体   繁体   中英

Using UISegmentedControl with UISearchController

How do i use SegmentedControl with UISearchController ? I need the SegmentedControl to be under the SearchBar when SearchBar is inactive and hide the segmented control when SearchBar is activated.

I put SearchController.searchBar to the tableview header, so the SegmentedControl which i defined in storyboard is overlapped by SearchBar . When i tried to add SegmentedControl programmatically in viewDidLoad by calling tableView.tableHeaderView?.addSubview(customSecmentedControl), it overlapped the cells in tableView.

You don't have to wire up your own segmented control to handle this, as that functionality is already provided by UISearchController .

Use the search bar's scopeButtonTitles property to set the titles for your scope bar:

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

You can determine the selected scope by accessing the search bar's selectedScopeButtonIndex property.

The search bar sends its delegate notifications when the scope changes:

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

More details about the scope bar are provided in the UISearchBar and UISearchBarDelegate documentation.

This worked for me.

    let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
        super.viewDidLoad()

        searchController.searchBar.showsScopeBar = true

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM