【问题标题】:keep the UISearchBarDisplayController on Top of tableview将 UISearchBarDisplayController 保持在 tableview 的顶部
【发布时间】:2023-03-12 01:42:02
【问题描述】:

我正在尝试快速将 UISearchBarDisplayController 保持在 UItableview 之上 我尝试了这段代码(我从 objectC 转换为 swift)但没有运气:

    override func scrollViewDidScroll(scrollView: UIScrollView) {
//        
        var searchBar: UISearchBar  = (self.searchDisplayController?.searchBar)!
//
        var rect = searchBar.frame

        self.searchDisplayController?.searchBar.frame = CGRectMake(0, max(0, scrollView.contentOffset.y), rect.width, rect.height);
    }

当我滚动表格时,搜索栏仍会随着 tableview 滚动。有谁可以帮我解决它?谢谢

【问题讨论】:

    标签: uitableview swift uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我发现我必须创建一个新的视图控制器,添加一个 tableview 并在 tableview 之外添加一个 searchBar。并且搜索栏不会随着 tableview 滚动

    【讨论】:

      【解决方案2】:

      在 Swift 2.1、iOS 9.2.1 和 Xcode 7.2 中

      let searchController = UISearchController(searchResultsController: nil)
      
              override func viewDidLoad() {
              /* Search controller parameters */
                  searchController.searchResultsUpdater = self  // This protocol allows your class to be informed as text changes within the UISearchBar.
                  searchController.dimsBackgroundDuringPresentation = false  // In this instance,using current view to show the results, so do not want to dim current view.
                  definesPresentationContext = true   // ensure that the search bar does not remain on the screen if the user navigates to another view controller while the UISearchController is active.
      
                  let tableHeaderView: UIView = UIView.init(frame: searchController.searchBar.frame)
                  tableHeaderView.addSubview(searchController.searchBar)
                  self.tableView.tableHeaderView = tableHeaderView
      
              }
              override func scrollViewDidScroll(scrollView: UIScrollView) {
                 let searchBar:UISearchBar = searchController.searchBar
                 var searchBarFrame:CGRect = searchBar.frame
                 if searchController.active {
                    searchBarFrame.origin.y = 10
                 }
                 else {
                   searchBarFrame.origin.y = max(0, scrollView.contentOffset.y + scrollView.contentInset.top)
      
                 }
                 searchController.searchBar.frame = searchBarFrame
             }
      

      【讨论】:

      • 这将在滚动时将搜索栏保持在顶部。但是如果我滚动并尝试点击搜索栏,以激活搜索栏,当搜索栏下方有一个表格视图单元格时,它将选择 tableViewCell 而不是搜索栏。
      猜你喜欢
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多