How to add the UISegmentedControl in UINavigationBar?

前端 未结 6 1369
遥遥无期
遥遥无期 2021-02-04 03:44

I have tried to add the UISegmentedControl to the bottom of UINavigationBar with title. But i cannot add it and I cannot add UISegmentedControl

6条回答
  •  春和景丽
    2021-02-04 04:41

    You can also add the segment control to a search bar, not the navigation item. If you're like me, I needed a large title + search bar + navigation item, which would be impossible with the current top answer.

    @Abhishek 's answer is close. You would do something like the following:

    // Create the search controller
    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.scopeButtonTitles = ["Option 1", "Option 2"]
    
    // Make sure the scope bar is always showing, even when not actively searching
    searchController.searchBar.showsScopeBar = true
    
    // Make sure the search bar is showing, even when scrolling
    navigationItem.hidesSearchBarWhenScrolling = false
    
    // Add the search controller to the nav item   
    navigationItem.searchController = searchController
    definesPresentationContext = true
    

    @Gurjit Singh, the line with .showsScopeBar = true is the solution to your problem.

提交回复
热议问题