【问题标题】:How to add a searchBar with scopebar inside of a NavigationBar where the navBar automatically increase its height and shows the scopeBar如何在导航栏内添加带有范围栏的搜索栏,其中导航栏自动增加其高度并显示范围栏
【发布时间】:2017-10-25 03:34:20
【问题描述】:

我阅读了有关 stackoverflow 的所有问题,但没有一个可以解决我的问题。我创建了一个 UICollectionView 并在导航栏中锚定了一个搜索栏,而不使用情节提要!

   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

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

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

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

将我的代码添加到一个简单的 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 如下所示。如果您开始键入范围栏,则会出现,如果您单击取消,它就会消失。我想要相同的,但只在 navBar 内部:

如何在没有情节提要的情况下在导航栏中添加带有 scopeBar 的 searchBar。有没有简单的方法。请您参考我的代码并使用我的代码向我展示它是如何工作的。我还尝试增加导航栏的大小,但它应该会自动工作,我不知道这是否是正确的方法。

【问题讨论】:

  • 您可以尝试放弃scopeBar,并在搜索栏下方添加UISegmentController,以根据所选细分过滤您的搜索
  • 我为我的 collectionView 创建了一个标题,并在我的标题中添加了分段控件。问题是我无法根据所选段更改我的 collectionView 的内容。我已经问过这个问题,他们建议使用 searchBar 中的 scopeBar 属性,现在您建议使用 segmentedControl :)
  • 哦,我很抱歉,我只是想提供一个不同的选择。您能否详细说明为什么您不能根据所选细分更改搜索内容?我看看能不能帮忙
  • 感谢您的帮助。该问题在上面的链接中进行了描述。你能回答这个问题吗?

标签: ios swift uinavigationcontroller uisearchbar uisearchbardelegate


【解决方案1】:

我认为这是您正在寻找的那种实现:

你的段控制器

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

然后您必须使用您的UICollectionView 注册您的自定义UICollectionViewCell 类。在您的 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()
} 

【讨论】:

  • 谢谢。我完全按照您之前描述的方式进行了尝试。问题是collectionVIew的内容没有改变,因为没有分段控件的目标动作方法。我不知道你可以把那个功能放在哪里。我希望你能帮助我,因为这个问题让我从 HOURS 开始就头疼。
  • 我们应该说类似 sc.addTarget(self, action: #selector(segmentedChange), for: .valueChanged) 然后执行 collectionView.reloadData 但不知道在我们目前的情况下将这段代码放在哪里
  • 但是我们如何从头类中重新加载collectionView的数据。我在 headerClass 中创建了 segmentedControl 作为全局变量
  • 阅读更新的答案,如果您还有任何问题,请告诉我
  • 谢谢阁楼。将目标添加到名为 sc 的全局定义的 segmentedControl 时留下以下问题: sc.addTarget(self, action: #selector(#selector(UserSearchController.filterChanged(_:))), for: .touchUpInside) --> 它说使用未解析的标识符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 2017-12-30
  • 1970-01-01
相关资源
最近更新 更多