ios - Auto-sizing UICollectionView headers -
i'm trying make detail screen to-do list kind of app. here's detail screen looks like:
this uicollectionviewcontroller
, header. header contains 2 uilabel
objects, , uitextview
object. layout of these objects managed vertical uistackview
. uiview
used set white background.
i'm having difficulties in defining height of uicollectionreusableview
@ runtime. advice appreciated.
here how can handle custom uicollectionviewreusableview auto layout without xib file.
- implement
referencesizeforheaderinsection
delegate method. - in it, instantiate view use header view.
- set visibility hidden, avoid flashing.
- add view collectionview's superview.
- set it's layout using auto layout, match expected visual outcome of header.
- invoke
setneedslayout
,layoutifneeded
- remove view superview
caution: not big fan of solution, adds custom view collectionview's superview each time, perform calculations. didn't notice performance issues though.
caution #2: i'd treat temporary solution, , migrate self sizing supplementary views once published.
i using purelayout autolayout purposes.
func collectionview(_ collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, referencesizeforheaderinsection section: int) -> cgsize { let header = customheaderview() header.ishidden = true; self.view.addsubview(header) header.autopinedge(tosuperviewedge: .leading) header.autopinedge(tosuperviewedge: .trailing) header.autopin(totoplayoutguideof: self, withinset: 0) header.setupheader(withdata: self.data) header.setneedslayout() header.layoutifneeded() header.removefromsuperview() return header.frame.size }
Comments
Post a Comment