如何在NavigationBar中添加带有范围栏的searchBar,其中navBar会自动增加其高度并显示scopeBar

时间:2017-05-24 16:49:03

标签: ios swift uinavigationcontroller uisearchbar uisearchbardelegate

我阅读了有关stackoverflow的所有问题,但没有一个能解决我的问题。我创建了一个UICollectionView,并在不使用Storyboard的情况下将navigationBar锚定在navigationBar中!

   class UserSearchController: UICollectionViewController ,.. {..
    lazy var  searchBar: UISearchBar = {
    let sb = UISearchBar()
    sb.placeholder = "Enter username"
    sb.barTintColor = UIColor.gray
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
    sb.delegate = self
    sb.showsCancelButton = true
    return sb
}()  .... 

  // adding the searchBar to the collectionView as subView and      
    anchoring it  to the navigationBar

enter image description here

搜索栏显示在navigationBar中,一切正常。当我尝试将范围栏添加到我的搜索栏时,会出现此问题。我将以下属性添加到我的搜索栏

      sb.showsScopeBar = true
      sb.scopeButtonTitles = ["Username", "Hashtag"]

scopeBar隐藏在navigationBar后面,navBar不会自动增加其高度。你只看到一个灰色的背景。

enter image description here

将我的代码添加到简单的UiViewController一切正常

此代码在普通的VIewController中工作,其中searchBar作为subView添加并锚定到视图。

    lazy var  searchBar: UISearchBar = {

    let sb = UISearchBar()
    sb.placeholder = "Enter username"
    sb.barTintColor = UIColor.gray
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
    sb.delegate = self
    sb.scopeButtonTitles = ["Username", "Hashtag"]
    sb.tintColor = UIColor.black
    sb.barTintColor = UIColor.lightGray


    return sb
}()

public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsScopeBar = true
    searchBar.sizeToFit()
    searchBar.setShowsCancelButton(true, animated: true)

    return true
}



func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.showsScopeBar = false
    searchBar.endEditing(true)
}

我的searchBar如下所示。如果您开始键入,则会出现scopeBar,如果单击取消,它将消失。我想要相同但只在navBar里面:

enter image description here enter image description here

如何在没有Storyboard的情况下在navigationBar中添加带有scopeBar的searchBar。难道没有任何简单的方法。请您参考我的代码并使用我的代码向我展示这是如何工作的。我也试图增加navigationBar的大小,但它应该自动工作,我不知道这是否是正确的方法。

1 个答案:

答案 0 :(得分:0)

我认为这是您正在寻找的那种实施方式:

您的分段控制器

// ( Declare Globaly )
var yourSegmentController = UISegmentedControl()
yourSegmentController.addTarget(self, action: #selector(ParentClass.filterChanged(_:)), for: .touchUpInside)

然后,您必须使用UICollectionViewCell注册自定义UICollectionView课程。在viewDidLoad中执行此操作,或者在UICollectionView内部使用的模块中使用任何初始化方法:

self.yourCollectionView.register(YourCustomUserCellClassHere.self, forCellWithReuseIdentifier: NSStringFromClass(YourCustomUserCellClassHere))

self.yourCollectionView.register(YourCustomHashtagCellClassHere.self, forCellWithReuseIdentifier: NSStringFromClass(YourCustomHashtagCellClassHere))

最后,在您的cellForItemAt委托方法中,执行以下操作以查看选择了哪个过滤器并相应地返回单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    var cellToReturn = UICollectionViewCell()

    // USERS
    if yourSegmentController.selectedSegmentIndex == 0 {

        let customUserCell = self.yourCollectionView.dequeueReusableCell( withReuseIdentifier: NSStringFromClass(YourCustomUserCellClassHere), for: indexPath) as? YourCustomUserCellClassHere

        // Do what you want with your custom cell here...


        cellToReturn = customUserCell
    }
    // HASHTAGS
    else if yourSegmentController.selectedSegmentIndex == 1 {

        let customHashtagCell = self.yourCollectionView.dequeueReusableCell( withReuseIdentifier: NSStringFromClass(YourCustomHashtagCellClassHere), for: indexPath) as? YourCustomHashtagCellClassHere

        // Do what you want with your custom cell here...


        cellToReturn = customHashtagCell

    }

    return cellToReturn

}

这将检查选择了哪个过滤器,然后返回适合过滤器的单元格。

另请注意,每次用户更改细分过滤器时,您都必须刷新UICollectionView

像这样刷新它:

// Put this method at the same level where your cellForItemAt function is but not inside

func filterChanged (_ sender:UISegmentedControl) {
    self.yourCollectionView.reloadData()
}