【问题标题】:Swift - UISearchController Does Not Activate - No Cursor - No KeyboardSwift - UISearchController 不激活 - 没有光标 - 没有键盘
【发布时间】:2021-11-07 07:38:42
【问题描述】:

我有一个 UICollectionView,里面有一个标题视图,导航栏的标题有一个 UISearchController。此代码适用于其他视图,但不适用于此 UICollectionView。我想把这个扔给 SO 社区,看看他们是否可以帮助我解决这个问题。

class ViewBusinessProfile: UICollectionViewController, UICollectionViewDelegateFlowLayout,  CLLocationManagerDelegate, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {

 @IBOutlet var collectionViews: UICollectionView!

   
 var searchController : UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.searchController = UISearchController(searchResultsController:  nil)
    self.searchController.searchResultsUpdater = self
    self.searchController.delegate = self
    self.searchController.searchBar.delegate = self
    self.searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.dimsBackgroundDuringPresentation = true
    self.searchController.obscuresBackgroundDuringPresentation = false
    self.searchController.searchBar.placeholder = "search products..."
    //self.searchController.automaticallyShowsCancelButton = false
    self.searchController.definesPresentationContext = true
    searchController.searchBar.becomeFirstResponder()

    navigationItem.titleView = searchController.searchBar
    
    collectionViews.dataSource = self
    collectionViews.delegate = self

}

 func updateSearchResults(for searchController: UISearchController) {
}

我可以做些什么来调试或尝试追踪此问题?我在新的 UICollectionView 中重新创建了视图,并且出现了同样的问题。 SearchBar 是可见的,但无法触摸但 Back 按钮在导航栏上有效。

这三个视图都有 UISearchControllers,我在它们之间进行切换。最右边的那个就是发生这个问题的地方。左侧两个视图使用了相同的 UISearchController 代码,效果很好。

【问题讨论】:

    标签: swift uisearchcontroller


    【解决方案1】:

    根据我看到的in this related solution,我想出了这个代码:

    import UIKit
    
    class SecondViewController: UIViewController, UISearchControllerDelegate, UISearchBarDelegate {
    
        var searchController : UISearchController!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            addNavigationBarItems()
       }
    
        func addNavigationBarItems() {
    
            self.searchController = searchControllerWith(searchResultsController: nil)
            navigationItem.titleView = self.searchController.searchBar
    
            self.definesPresentationContext = true
        }
    
        func searchControllerWith(searchResultsController: UIViewController?) -> UISearchController {
    
            let searchController = UISearchController(searchResultsController: searchResultsController)
            searchController.delegate = self
            // searchController.searchResultsUpdater = self
            searchController.searchBar.delegate = self
            searchController.hidesNavigationBarDuringPresentation = false
            // dimsBackgroundDuringPresentation is deprecated as of iOS 12.0
            searchController.dimsBackgroundDuringPresentation = true
            searchController.searchBar.searchBarStyle = .prominent
            searchController.searchBar.backgroundImage = UIImage()
            searchController.searchBar.barTintColor = UIColor(red: 10/255, green: 150/255, blue: 255/255, alpha: 1) //rgb(10, green: 150, blue: 255)
    
            return searchController
        }
    
        func updateSearchResults(for searchController: UISearchController) {
        }
    
    }
    

    这假设父视图控制器中已经存在导航栏(即您不必创建新的 NavigationBar),结果应该如下所示:

    【讨论】:

    • 使用评论中提供的代码,搜索栏现在不会出现,并且 UICollectionView 在图像约束方面存在偏差
    • 它出现在视图层次结构中,也许我需要增加尺寸
    • 这样解决方案就可以了。问题是搜索栏似乎非常小并且位于当前导航栏的后面。点击默认导航栏的底部会弹出一个键盘,因此搜索栏非常小,位于默认导航栏的后面。
    • 您可能需要使用一些自动布局 (like what's seen here) 使其更宽。
    • 所以问题是你帮我创建的搜索栏在默认导航栏后面。如何将创建的搜索栏/导航栏放在默认导航栏的前面?还是禁用默认导航栏?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2021-11-10
    相关资源
    最近更新 更多