UISearchController搜索框向下移动

g0l*_*d2k 12 ios swift uisearchcontroller

我有一个实现UISearchBarDelegate的UITableVIewController,视图嵌入在导航控制器中.

    class FacilityTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate, AmenityFilterDelegate {

        // MARK: - Public Variables

        var targetFacilities = [Int]()
        var searchController: UISearchController = UISearchController(searchResultsController: nil)

        // MARK: - Private Variables

        private var viewModel: FacilityTableViewModel!
        private let parkGreenColor = UIColor(red: 73/255, green: 136/255, blue: 84/255, alpha: 1)
        private var showEmptyMessage = false

        // MARK: - View Lifecycle

        /**
        Setup view after loading
        */
        override func viewDidLoad() {
            super.viewDidLoad()

            trackScreenView("Facility Table View")

            if targetFacilities.isEmpty {
                viewModel = FacilityTableViewModel()
            } else {
                viewModel = FacilityTableViewModel(facilityIds: targetFacilities)
            }

            // Seup search controller
            searchController.searchResultsUpdater = self
            searchController.dimsBackgroundDuringPresentation = false
            searchController.hidesNavigationBarDuringPresentation = false
            searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, searchController.searchBar.frame.size.width, 44)
            searchController.searchBar.tintColor = UIColor.whiteColor()
            searchController.searchBar.barTintColor = parkGreenColor
            searchController.searchBar.translucent = false

            self.definesPresentationContext = true

            tableView.tableHeaderView = searchController.searchBar
        }
Run Code Online (Sandbox Code Playgroud)

在点击搜索之前

我发现当我禁用导航栏的半透明属性时,搜索框会将其位置向下移动.

点击搜索后

如果我设置了definesPresentationContext = false那么搜索栏不会向下移动,但是如果我在搜索框中输入文本并选择其中一个结果,则无法打开生成的模态窗口.我收到以下错误:

搜索结果

    2015-03-17 15:06:56.101 VB ParkFinder[16368:2667719] Warning: Attempt to present <UINavigationController: 0x7fa2f9ced930>  on <VB_ParkFinder.FacilityTableViewController: 0x7fa2f9c27ba0> which is already presenting (null)
Run Code Online (Sandbox Code Playgroud)

以下是我的segue代码:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let navController = segue.destinationViewController as UINavigationController
        if segue.identifier == "facilityDetailsSegue" {
            let detailsViewController = navController.childViewControllers.last as FacilityDetailsViewController

            if let indexPath = tableView.indexPathForSelectedRow() {
                var facilityId: Int
                if searchController.active {
                    facilityId = viewModel.idForSearchResultsAtIndexPath(indexPath)
                } else {
                    facilityId = viewModel.idForFacilityAtIndexPath(indexPath)
                }

                detailsViewController.currentFacilityId = facilityId
            }
        } else if segue.identifier == "FilterPopover" {
            let aftvc = navController.childViewControllers.last as AmenityFilterTableViewController
            aftvc.delegate = self
        }
    }
Run Code Online (Sandbox Code Playgroud)

我迷失了该怎么做.我想要关闭半透明的导航栏,我需要能够从搜索结果中启动模态窗口.有关如何实现这一点的任何想法?

小智 6

我有同样的问题,看一看

嵌入导航栏时,iOS 7中的奇怪UISearchDisplayController视图偏移行为

这解决了我的问题.

猜猜它可能被标记为重复,不知道如何做到这一点.

  • 为了节省您的时间:链接通常是关于具有表格视图且视频栏导致问题的视图控制器将"在顶部条形图下"和"在不透明条形图下"设置为TRUE. (3认同)