swift - UICollectionView Section headers & footers show wrong way - iOS -
the problem is: supplementaryview
has textlabel
on it, text appears @ wrong location while header , footer , textlabel show right.
as can see in picture, first section header has title, other header or footers text out of sight(they on screen have scroll down see it).
don't know how solve it...
i setup collectionview following in viewdidload
function:
func setupcollectionsview(){ let width:cgfloat = self.view.frame.size.width/4 let flowlayout = uicollectionviewflowlayout() flowlayout.minimuminteritemspacing = 5 flowlayout.minimumlinespacing = 5 flowlayout.estimateditemsize = cgsizemake(width, width) flowlayout.headerreferencesize = cgsizemake(self.view.frame.size.width, 30) flowlayout.footerreferencesize = cgsizemake(self.view.frame.size.width, 30) flowlayout.scrolldirection = uicollectionviewscrolldirection.vertical collectionview = uicollectionview(frame: self.view.frame, collectionviewlayout: flowlayout) collectionview.datasource = self collectionview.delegate = self collectionview.registerclass(funcviewcell.self, forcellwithreuseidentifier: "collectionview") collectionview.backgroundcolor = uicolor.clearcolor() collectionview.registerclass(supplementaryview.self, forsupplementaryviewofkind: uicollectionelementkindsectionheader, withreuseidentifier: "headerview") collectionview.registerclass(supplementaryview.self, forsupplementaryviewofkind: uicollectionelementkindsectionfooter, withreuseidentifier: "footerview") self.view.addsubview(collectionview) }
for each supplementaryview write this:
func collectionview(collectionview: uicollectionview, viewforsupplementaryelementofkind kind: string, atindexpath indexpath: nsindexpath) -> uicollectionreusableview { var view:supplementaryview? var title:string = "" switch kind { case uicollectionelementkindsectionfooter: view = collectionview.dequeuereusablesupplementaryviewofkind(kind, withreuseidentifier: "footerview", forindexpath: indexpath) as? supplementaryview view?.backgroundcolor = uicolor ( red: 0.9879, green: 0.3225, blue: 0.4925, alpha: 1.0 ) title = "footer" default: view = collectionview.dequeuereusablesupplementaryviewofkind(kind, withreuseidentifier: "headerview", forindexpath: indexpath) as? supplementaryview view?.backgroundcolor = uicolor ( red: 0.6571, green: 1.0, blue: 0.8628, alpha: 1.0 ) title = "header" } switch indexpath.section { case 0: title = "first \(title)" default: title = "second \(title)" } view?.settitle(title) return view! }
and supplementary view implemented way:
class supplementaryview: uicollectionreusableview{ var titlelabel:uilabel! override init(frame: cgrect) { super.init(frame: frame) titlelabel = uilabel(frame: frame) self.addsubview(titlelabel) let gesture = uitapgesturerecognizer(target: self, action: #selector(supplementaryview.tap)) self.addgesturerecognizer(gesture) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } func settitle(title:string){ print("label's frame is\(titlelabel.frame)") titlelabel.text = title } func tap(){ print("\(titlelabel.text!) tapped") }}
change code below, , see if works
override init(frame: cgrect) { super.init(frame: frame) // there should self.bounds titlelabel = uilabel(frame: self.bounds) self.addsubview(titlelabel) let gesture = uitapgesturerecognizer(target: self, action: #selector(supplementaryview.tap)) self.addgesturerecognizer(gesture) }
Comments
Post a Comment