【发布时间】:2016-03-01 06:56:50
【问题描述】:
目标:拥有一个始终可见的搜索栏。
- 视图控制器嵌入在导航控制器中
- 不存在表视图
- 当用户点击搜索栏时,搜索栏需要像 iOS 联系人应用程序一样,从当前位置移动到导航栏顶部(向上) .
- 使用
UISearchController
我做了什么:
- 我已将搜索栏添加到视图控制器的视图中
- 我手动显示搜索视图控制器(代码如下)
问题:
- 我无法控制动画,目前搜索栏从屏幕顶部向下移动到导航栏的位置。
预期行为:
- 能够通过将搜索栏从当前位置向上移动到导航栏来为搜索栏设置动画
问题
- 我的方法正确吗?
- 如何设置动画并将搜索栏从当前位置向上移动?
代码:
func setupSearchController() {
searchController.searchResultsUpdater = self
self.definesPresentationContext = false
searchController.delegate = self
searchController.hidesNavigationBarDuringPresentation = true
let searchBar = searchController.searchBar
view.addSubview(searchBar)
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
searchBar.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
searchBar.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true
}
func presentSearchController(searchController: UISearchController) {
let searchBar = searchController.searchBar
searchBar.removeFromSuperview()
let baseView = searchController.view
baseView.addSubview(searchBar)
searchBar.leadingAnchor.constraintEqualToAnchor(baseView.leadingAnchor).active = true
searchBar.trailingAnchor.constraintEqualToAnchor(baseView.trailingAnchor).active = true
searchBar.topAnchor.constraintEqualToAnchor(searchController.topLayoutGuide.bottomAnchor).active = true
self.presentViewController(searchController, animated: true) {
}
}
【问题讨论】:
-
您的导航栏在整个应用程序中是否相同?
-
是的,它是一个简单的应用程序,只有主屏幕。搜索栏位于主屏幕上。
-
你什么时候打电话给
setupSearchController和presentSearchController -
“viewDidLoad”中的“setUpSearchController”和“presentSearchController”是一个“UISearchControllerDelegate”方法
-
根据我的经验,UISearchController 是非常新的(错误的)并且用例非常严格(那些可以工作的)。我花了(失去)很多时间玩它。要么不做动画,要么尝试以某种方式伪造演示文稿:自己部分动画搜索栏,然后用简单的淡入淡出呈现搜索控制器。无论如何都没有简单的解决方案..
标签: ios swift uitableview uisearchcontroller