UISearchController的searchBar不会填满全宽

时间:2016-01-13 21:00:02

标签: ios iphone swift uitableview uisearchcontroller

编辑:我正在使用Autolayout。

使用UISearchController的searchBar填充屏幕的整个宽度时遇到问题。我的设置是这样的:

  1. 我有一个UIViewController。在里面,我将所有元素分开。有一个显示结果的UITableView。
  2. 由于我遇到了与状态栏重叠的searchBar,我创建了一个具有约束的普通UIView,以便它位于状态栏下并填充UIViewController的整个宽度。这是我在这里见过很多次的方法。
  3. 最后,我创建了UISearchController并将其添加为" wrapperView"
  4. 的子视图

    结果如下:

    The blue is only to see to where the container view extends

    对我来说,searchBar要么没有正确填充容器,要么没有正确居中,因为左边的边看起来不像另一边那么宽。

    此外,当搜索框处于活动状态时,所有内容都显示正常:

    The table also displays fine

    以下是我进行设置的代码:

    //Mark: - Outlets
    @IBOutlet weak var wrapperView: UIView!
    @IBOutlet weak var tableView: UITableView!
    
    //Mark: - Properties
    let searchController = UISearchController(searchResultsController: nil)
    
    override func viewDidLoad() {
        /*!
        Sets the values of the searchController and it's behaviour
        */
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.delegate = self
    
        /*!
        Sets the values and behaviours of the tableView
        */
        tableView.delegate = self
        tableView.dataSource = self
        tableView.hidden = true
    
        definesPresentationContext = true
        searchController.searchBar.sizeToFit()
        searchController.searchBar.frame.size.width = self.view.frame.size.width
    }
    

    此外,我的搜索逻辑工作正常。

1 个答案:

答案 0 :(得分:-1)

显然只有模拟器严重显示搜索栏。我不知道这是否是模拟器的错误,但在设备上它工作正常!我会在此将这个答案标记为正确答案,希望它能成为其他人的好参考!干杯!

相关问题