【问题标题】:Why UISearchController disappears when typing on UISearchBar?为什么在 UISearchBar 上输入时 UISearchController 会消失?
【发布时间】:2017-01-06 11:14:42
【问题描述】:
func configureSearchController() {
    // Initialize and perform a minimum configuration to the search controller.
    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()

    // Add search bar to View.
    self.view.addSubview(searchController.searchBar)

    searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false

    view.bringSubview(toFront: searchController.searchBar)

    //add constraints on searchbar
    view.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0))

    view.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 64))

}

这就是我的代码,我添加了一个 searchController 搜索栏作为视图的子视图。我也添加了约束。

每当我在搜索栏中输入内容时,整个内容都会消失,导航栏也会以动画方式消失。

首先是这样的

然后当我开始输入时,它就消失了

【问题讨论】:

  • 它是 UISearchController 的默认行为,你可以附上截图。
  • 尝试使用此设置 self.extendedLayoutIncludesOpaqueBars = true 或 self.automaticallyAdjustsScrollViewInsets = true
  • 不行

标签: ios swift xcode uisearchbar uisearchcontroller


【解决方案1】:

searchController1.hidesNavigationBarDuringPresentation = false

【讨论】:

    【解决方案2】:

    将以下内容添加到您的 ViewController

        definesPresentationContext = true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      相关资源
      最近更新 更多