UISearchController - 搜索栏应始终可见

时间:2016-03-01 06:56:50

标签: ios swift uitableview uisearchcontroller

目标:要使搜索栏始终可见。

  1. 视图控制器嵌入在导航控制器中
  2. 不存在表格视图
  3. 当用户点按搜索栏时,搜索栏需要从当前位置动画到导航栏的顶部(向向上移动),就像在 iOS联系人应用中一样
  4. 使用UISearchController
  5. 我做了什么:

    1. 我已将搜索栏添加到视图控制器的视图
    2. 我手动呈现搜索视图控制器(下面的代码)
    3. 问题:

      • 我无法控制动画,目前搜索栏将向下从屏幕顶部移动到导航栏的位置。

      预期行为:

      • 能够通过将搜索栏向上从当前位置移动到导航栏来设置搜索栏的动画

      问题

      1. 我的方法是否正确?
      2. 如何设置动画并从当前位置向上移动搜索栏?
      3. 代码:

            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) {
        
                }
            }
        

1 个答案:

答案 0 :(得分:7)

我并没有完全遵循你的逻辑,但我能说的是你可以很容易地实现这一点。

解决方案

  • 界面生成器上创建UIView将是容器 searchController' searchBar的约束,并将约束设置为 以下:

enter image description here

  • UIViewUIViewController相关联,并将其命名为searchBarContainer
  • 创建您要使用的UISearchController属性:var searchController: UISearchController! = nil
  • searchController.searchBar 添加到 searchBarContainer

幸运的是你的控制器看起来应该是这样的:(注意,这里我没有在这里实现代理,但你可以照顾它,因为这与动画无关:)

class SearchViewController: UIViewController {
    var searchController: UISearchController! = nil
    @IBOutlet weak var searchBarContainer: UIView!

    // MARK: - View Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        self.searchBarContainer.addSubview(searchController.searchBar)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        // Setup the frame each time that the view appears in order to fit the width of the screen
        // If you use autolayout just call searchController.searchBar.layoutIfNeeded()
        var frame = searchController.searchBar.bounds
        frame.size.width = self.view.bounds.size.width
        searchController.searchBar.frame = frame
    }
}

输出

我的解决方案

enter image description here

iOS通讯录

enter image description here

相关问题