ios - Since inserting UITableViewAutomaticDimension returned cells are tiny -
i working on comment section app , want cells resize automatically. if replace uitableviewautomaticdimension
arbitrary value 120, looks more or less want to.
however, if leave @ uitableviewautomaticdimension
cells returned literally tiny. add picture @ end showing both ways (left: uitableviewautomaticdimension, right: rowheight = 120). how can fix this? haven't found similar problem since do set constraints, in many cases cause of auto-resize problems (the subviews have translatesautoresizingmaskintoconstraints
set false
).
i going provide code may of interest. it's nothing else standard table view cells supposed auto-resize , use constraints.
i appreciate help!
commentcell
override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) setupviews() } func setupviews() { //all of these subviews have .translatesautoresizingmaskintoconstraints = false contentview.addsubview(profilepictureview) contentview.addsubview(usernamelabel) contentview.addsubview(commentlabel) let marginguide = contentview.layoutmarginsguide let viewwidth = uiscreen.main.bounds.width nslayoutconstraint.activate([ profilepictureview.heightanchor.constraint(equaltoconstant: 42), profilepictureview.widthanchor.constraint(equaltoconstant: 42), profilepictureview.leftanchor.constraint(equalto: marginguide.leftanchor), profilepictureview.topanchor.constraint(equalto: marginguide.topanchor, constant: -2), usernamelabel.leftanchor.constraint(equalto: profilepictureview.rightanchor, constant: 16), usernamelabel.widthanchor.constraint(equaltoconstant: viewwidth - 66), usernamelabel.centeryanchor.constraint(equalto: profilepictureview.centeryanchor, constant: -8), commentlabel.leftanchor.constraint(equalto: profilepictureview.rightanchor, constant: 16), commentlabel.widthanchor.constraint(equalto: usernamelabel.widthanchor), commentlabel.topanchor.constraint(equalto: usernamelabel.bottomanchor, constant: 4) ]) }
uitableviewautomaticdimension
| 120
:
the issue constraints - add constraints subviews of cell, not contentview. omitted height constraints usernamelabel
, commentlabel
- thats correct, resize according contents. contentview
not resize, because doesn't know desired size. without constraints, contentview
might deduce size, defaults 44.0
, described here.
so need add top constraint usernamelabel
, bottom constraint commentlabel
usernamelabel.topanchor.constraint(equalto: contentview.topanchor, constant: 0) commentlabel.bottomanchor.constraint(equalto: contentview.bottomanchor, constant: 0)
edit
seem have top anchor, bottom 1 should suffice.
Comments
Post a Comment