【发布时间】:2016-04-24 13:07:52
【问题描述】:
我有一个应用程序,它显示嵌入在 UINavigationController 中的 MKMapView。在 UINavigationController 我放了一个 UISearchController。当用户触摸 UISearchController 时,它会显示一个 UITableViewController。 当我没有在 UISearchController 中添加 Scope 按钮时,它运行良好。
这里是我启动App时UINavigationController中UISearchController的截图。
接下来,当我触摸 UISearchController 时,它会显示 UITableViewController 和范围按钮。
在这里我们已经可以看到范围按钮存在问题,因为它们没有很好地集成到 UISearchController 中(颜色应该是半透明的)
接下来,当我点击取消按钮返回主视图控制器时,UISearchController 并没有恢复其原始样式
它有一个深灰色边框(可能来自范围按钮)。
这是我在主视图控制器中添加 UISearchController 的方法
func initSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController
self.searchController = UISearchController(searchResultsController: mySearchController)
mySearchController.theSearchController = self.searchController
mySearchController.delegate = self
// Configure the UISearchController
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.placeholder = "data.."
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
此方法在我的主视图控制器的 viewDidLoad() 中调用。
接下来,当 SearchController 显示出来时,我在 TableViewController 子类中添加了范围按钮,代码如下
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.hidden = false
var rect = delegate.searchController.searchBar.superview?.frame
rect?.size.height = 88
self.delegate.searchController.searchBar.scopeButtonTitles = ["one", "two", "three"]
self.delegate.searchController.searchBar.showsScopeBar = true
self.delegate.searchController.searchBar.superview?.frame = rect!
}
当搜索关闭时执行以下代码
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
var rect = delegate.searchController.searchBar.superview?.frame
rect?.size.height = 44
self.delegate.searchController.searchBar.superview?.frame = rect!
self.delegate.searchController.searchBar.showsScopeBar = false
self.delegate.searchController.searchBar.scopeButtonTitles = nil
}
如您所见,这段代码有几个问题。
- 范围按钮显示不正确,我无法用漂亮的动画添加它们
- 当用户退出时,搜索范围按钮被移除,但它会影响 UISearchController 的背景
你能告诉我我做错了什么,我应该怎么做才能在 UISearchController 中正确集成 Scope Button? 我找到了示例,但仅当 UISearchController 未嵌入 UINavigationController 时。
感谢您的帮助!
塞巴斯蒂安。
【问题讨论】:
标签: ios uinavigationcontroller uinavigationbar uisearchcontroller