ios – 如何让搜索栏在uicollectionview中工作

ios – 如何让搜索栏在uicollectionview中工作,第1张

概述我想在一个uicollection视图上方放置一个搜索栏,如下图所示.我想以编程方式执行此 *** 作. 目前来看 这是我的搜索栏设置功能代码.我在家庭视图控制器中有它. func setupSearchBar() { let searchBar = UISearchBar(frame: CGRect(x: 0, y: 64, width:UIScreen.main.bounds.width, 我想在一个uicollection视图上方放置一个搜索栏,如下图所示.我想以编程方式执行此 *** 作.

目前来看

这是我的搜索栏设置功能代码.我在家庭视图控制器中有它.

func setupSearchbar() {    let searchbar = UISearchbar(frame: CGRect(x: 0,y: 64,wIDth:UIScreen.main.bounds.wIDth,height: 32))    searchbar.barTintcolor = UIcolor(red: 64/255,green: 64/255,blue: 64/255,Alpha: 1)    searchbar.backgroundcolor = UIcolor.blue    searchbar.isTranslucent = true    searchbar.placeholder = "Search Timeline"    searchbar.searchbarStyle = UISearchbarStyle.prominent    vIEw.addSubvIEw(searchbar)}
解决方法 您可以将搜索栏添加到UICollectionvIEw标头中.

这将以编程方式创建searchbar

lazy var searchbar : UISearchbar = {    let s = UISearchbar()        s.placeholder = "Search Timeline"        s.delegate = self        s.tintcolor = .white        s.barTintcolor = // color you like        s.barStyle = .default        s.sizetoFit()    return s}()

接下来,在您的视图中,加载注册标题视图.

collectionVIEw?.register(UICollectionVIEwCell.self,forSupplementaryVIEwOfKind: UICollectionElementKindSectionheader,withReuseIDentifIEr: "headerCellID")

覆盖以下方法以定义您希望标题的高度.

func collectionVIEw(_ collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,referenceSizeforheaderInSection section: Int) -> CGSize {    return CGSize(wIDth: vIEw.frame.wIDth,height: 40)}

最后,将搜索栏添加到UICollectionvIEw标头,定义约束以适合整个标题视图.

overrIDe func collectionVIEw(_ collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableVIEw {    let header = collectionVIEw.dequeueReusableSupplementaryVIEw(ofKind: kind,withReuseIDentifIEr: "headerCellID",for: indexPath)        header.addSubvIEw(searchbar)        searchbar.translatesautoresizingMaskIntoConstraints = false        searchbar.leftAnchor.constraint(equalTo: header.leftAnchor).isActive = true        searchbar.rightAnchor.constraint(equalTo: header.rightAnchor).isActive = true        searchbar.topAnchor.constraint(equalTo: header.topAnchor).isActive = true        searchbar.bottomAnchor.constraint(equalTo: header.bottomAnchor).isActive = true    return header}
总结

以上是内存溢出为你收集整理的ios – 如何让搜索栏在uicollectionview中工作全部内容,希望文章能够帮你解决ios – 如何让搜索栏在uicollectionview中工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1067787.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存