【问题标题】:UISearchController - search bar should be visible alwaysUISearchController - 搜索栏应该始终可见
【发布时间】:2016-03-01 06:56:50
【问题描述】:

目标:拥有一个始终可见的搜索栏。

  1. 视图控制器嵌入在导航控制器中
  2. 不存在表视图
  3. 当用户点击搜索栏时,搜索栏需要像 iOS 联系人应用程序一样,从当前位置移动到导航栏顶部(向上) .
  4. 使用UISearchController

我做了什么:

  1. 我已将搜索栏添加到视图控制器的视图中
  2. 我手动显示搜索视图控制器(代码如下)

问题:

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

预期行为:

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

问题

  1. 我的方法正确吗?
  2. 如何设置动画并将搜索栏从当前位置向上移动?

代码:

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

        }
    }

【问题讨论】:

  • 您的导航栏在整个应用程序中是否相同?
  • 是的,它是一个简单的应用程序,只有主屏幕。搜索栏位于主屏幕上。
  • 你什么时候打电话给setupSearchControllerpresentSearchController
  • “viewDidLoad”中的“setUpSearchController”和“presentSearchController”是一个“UISearchControllerDelegate”方法
  • 根据我的经验,UISearchController 是非常新的(错误的)并且用例非常严格(那些可以工作的)。我花了(失去)很多时间玩它。要么不做动画,要么尝试以某种方式伪造演示文稿:自己部分动画搜索栏,然后用简单的淡入淡出呈现搜索控制器。无论如何都没有简单的解决方案..

标签: ios swift uitableview uisearchcontroller


【解决方案1】:

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

解决方案

  • 在您的 Interface Builder 上创建一个 UIView 将成为容器 的searchControllersearchBar 并将约束设置为 以下:

  • UIView 连接到 UIViewController 并将其命名为 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
    }
}

输出

我的解决方案

iOS 联系人

【讨论】:

  • 在这种情况下,当您点击搜索时,搜索栏不会以动画方式向上移动。它突然被放置在导航栏区域。在模拟器上 Debug > Slow Animations 或按 Command T 看看我的意思。当您取消时,您会看到流畅的动画,但当您点击搜索栏时,您将看不到任何动画。
  • 我希望它在点击搜索栏时具有动画效果(向上移动)。
  • 我看不出与 iOS 联系人应用程序有什么不同,这就是您想要的吧?或者你想要一个与 iOS Contact App 无关的自定义动画,就像我实现的那样?
  • 在通讯录应用中,点击搜索栏会有动画。搜索向上移动。在建议的解决方案中,搜索栏突然跳转(无动画)到导航栏区域。在您的模拟器上按 Command T 并尝试您的应用程序,然后尝试联系人应用程序。您将能够看到差异。
  • @user1046037 请检查输出的更新图像,并告诉我问题,以便我们修复它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多