将UISegmentedControl与UISearchController一起使用 [英] Using UISegmentedControl with UISearchController

查看:64
本文介绍了将UISegmentedControl与UISearchController一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 SegmentedControl UISearchController 一起使用?当 SearchBar 处于非活动状态时,我需要将 SegmentedControl 置于 SearchBar 之下,并在激活 SearchBar 时隐藏分段控件

我将SearchController.searchBar放在tableview标头中,因此我在情节提要中定义的 SegmentedControl SearchBar 覆盖.当我尝试通过调用tableView.tableHeaderView?.addSubview(customSecmentedControl)以编程方式在viewDidLoad中添加 SegmentedControl 时,它与tableView中的单元格重叠.

解决方案

您不必连接自己的分段控件来处理此问题,因为 UISearchController 已经提供了该功能./p>

使用搜索栏的 scopeButtonTitles 属性设置范围栏的标题:

  searchController.searchBar.scopeButtonTitles = [小",中",大"] 

您可以通过访问搜索栏的 selectedScopeButtonIndex 属性来确定所选范围.

范围更改时,搜索栏发送其委托通知:

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

UISearchBar UISearchBarDelegate 文档.

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.

这篇关于将UISegmentedControl与UISearchController一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆