ios - How can I programmatically make a UICollectionView fill a UITableViewCell? -
i'm trying simple, keep getting errors.
i have uitableview multiple sections. each section has header , 1 uitableviewcell. within each cell uicollectionview. of these views must created programmatically (no storyboard solutions whatsoever please).
the problem when try size , position uicollectionview. uicollectionview fill entire uitableviewcell, @ first tried size uicollectionview in uitableviewcell's init method using self.frame so:
var collectionview: uicollectionview? let collectionviewlayout: uicollectionviewflowlayout! override init(style: uitableviewcellstyle, reuseidentifier: string?) { self.collectionviewlayout = uicollectionviewflowlayout() self.collectionviewlayout.sectioninset = uiedgeinsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0) self.collectionviewlayout.scrolldirection = .vertical super.init(style: style, reuseidentifier: reuseidentifier) let frame = cgrect(x: 0.0, y: 0.0, width: self.frame.width, height: self.frame.height) self.collectionview = uicollectionview(frame: frame, collectionviewlayout: self.collectionviewlayout) self.collectionview?.datasource = self self.collectionview?.delegate = self }
however, think uitableviewcell's frame not set @ time (the init method call), , uicollectionview sized improperly. correct?
next, tried using layout anchors, so:
collectionview?.bottomanchor.constraint(equalto: self.bottomanchor).isactive = true
this gave me layout constraint error , said 'uicollectionviewproject[63851:2306772] [layoutconstraints] unable simultaneously satisfy constraints.' don't understand how thats possible. added 1 constraint, there nothing in storyboard, constraints possibly breaking?
somehow need make uicollectionview same size uitableviewcell , fit inside perfectly. again, must done programmatically only, no storyboards. please let me know need do. thanks.
in code have added 1 constraint
- bottomanchor
. autolayout
doesn't work that. add required constraints
after adding uicollectionview
subview
of uitableviewcell
, i.e.
override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) self.collectionviewlayout = uicollectionviewflowlayout() self.collectionviewlayout.sectioninset = uiedgeinsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0) self.collectionviewlayout.scrolldirection = .vertical let frame = cgrect(x: 0.0, y: 0.0, width: self.frame.width, height: self.frame.height) self.collectionview = uicollectionview(frame: frame, collectionviewlayout: self.collectionviewlayout) self.collectionview?.translatesautoresizingmaskintoconstraints = false self.addsubview(self.collectionview!) nslayoutconstraint.activate([ self.collectionview!.topanchor.constraint(equalto: self.topanchor), self.collectionview!.bottomanchor.constraint(equalto: self.bottomanchor), self.collectionview!.leftanchor.constraint(equalto: self.leftanchor), self.collectionview!.rightanchor.constraint(equalto: self.rightanchor), ]) self.collectionview?.datasource = self self.collectionview?.delegate = self }
Comments
Post a Comment