ios - Result with auto layout in a UITableView different when starting the app and once the app is running -
i have uitableview created in interface builder. inside dynamic cells. added constraints (this method called in tableview:cellforrowatindexpath
) :
@iboutlet weak var rowview: uiview! @iboutlet weak var flagimageview: uiimageview! @iboutlet weak var computedratelabel: uilabel! @iboutlet weak var historybutton: uibutton! func buildconstraintsfor() { let viewsdictionary = ["rowview": rowview, "flagimageview": flagimageview, "view1": computedratelabel, "historybutton": historybutton] rowview.layer.borderwidth = 1 flagimageview.layer.borderwidth = 1 computedratelabel.layer.borderwidth = 1 historybutton.layer.borderwidth = 1 rowview.layer.bordercolor = uicolor.blackcolor().cgcolor flagimageview.layer.bordercolor = uicolor.purplecolor().cgcolor computedratelabel.layer.bordercolor = uicolor.orangecolor().cgcolor historybutton.layer.bordercolor = uicolor.greencolor().cgcolor // remove constraints rowview.removeconstraints(rowview.constraints()) historybutton.removeconstraints(historybutton.constraints()) // add constraints rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|-[flagimageview]-[view1]-[historybutton]-|", options: nil, metrics: nil, views: viewsdictionary)) rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-1-[flagimageview]-1-|", options: nil, metrics: nil, views: viewsdictionary)) rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-1-[view1]-1-|", options: nil, metrics: nil, views: viewsdictionary)) rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-1-[historybutton]-1-|", options: nil, metrics: nil, views: viewsdictionary)) // size historybutton.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:[historybutton(==25)]", options: nil, metrics: nil, views: viewsdictionary)) rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:[rowview(<=68)]", options: nil, metrics: nil, views: viewsdictionary)) rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:[rowview(>=67)]", options: nil, metrics: nil, views: viewsdictionary)) flagimageview.addconstraint(nslayoutconstraint(item: flagimageview, attribute: .width, relatedby: .equal, toitem: flagimageview, attribute: .height, multiplier: 1, constant: 0)) }
when run app, following want
when scroll tableview , when rows disappear , reappear (or when go viewcontroller , come back) following:
i don't understand why. if add this:
let width = uiscreen.mainscreen().applicationframe.size.width rowview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:[rowview(==\(width))]", options: nil, metrics: nil, views: viewsdictionary))
it seems solve problem doesn't work anymore when go landscape.
did wrong when did layout?
edit:
when print description of constraints of rowview after adding them here get:
<nslayoutconstraint:0x17009f3b0 uiimageview:0x1701f5a00.leading == uitableviewcellcontentview:0x1701968c0.leadingmargin> <nslayoutconstraint:0x17009f6d0 h:[uiimageview:0x1701f5a00]-(nsspace(8))-[uilabel:0x13760f7f0'12 106,98']> <nslayoutconstraint:0x17009f9a0 h:[uilabel:0x13760f7f0'12 106,98']-(nsspace(8))-[uibutton:0x137634b60]> <nslayoutconstraint:0x17009dec0 uitableviewcellcontentview:0x1701968c0.trailingmargin == uibutton:0x137634b60.trailing> <nslayoutconstraint:0x170098920 v:|-(1)-[uiimageview:0x1701f5a00] (names: '|':uitableviewcellcontentview:0x1701968c0 )> <nslayoutconstraint:0x17009a4a0 v:[uiimageview:0x1701f5a00]-(1)-| (names: '|':uitableviewcellcontentview:0x1701968c0 )> <nslayoutconstraint:0x174098dd0 v:|-(1)-[uilabel:0x13760f7f0'12 106,98'] (names: '|':uitableviewcellcontentview:0x1701968c0 )> <nslayoutconstraint:0x174099550 v:[uilabel:0x13760f7f0'12 106,98']-(1)-| (names: '|':uitableviewcellcontentview:0x1701968c0 )> <nslayoutconstraint:0x170283c00 v:|-(1)-[uibutton:0x137634b60] (names: '|':uitableviewcellcontentview:0x1701968c0 )> <nslayoutconstraint:0x1702837a0 v:[uibutton:0x137634b60]-(1)-| (names: '|':uitableviewcellcontentview:0x1701968c0 )> <nslayoutconstraint:0x170283d90 v:[uitableviewcellcontentview:0x1701968c0(<=68)]> <nslayoutconstraint:0x170283de0 v:[uitableviewcellcontentview:0x1701968c0(>=67)]>
i post solution found might help. ram help.
indeed, when you're using auto layout programmatically, don't forget add settranslatesautoresizingmaskintoconstraints(false)
on every component adding constraint to.
in case had created constraints in interface builder, , needed remove uilabel rows during run time, had recreate constraints rows (such aspect ratio, width or height). wanted change placement of components in rows, didn't need recreate constraints flagimageview
or historybutton
long don't remove them.
but needed remove constraints rowview
adding new ones , removing some. rowview
contentview
of uitableviewcell
. removing constraints, removing constraints linked superview uitableviewcell
itself. contentview
not same size parent. needed add these 2 lines:
rowview.superview?.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|-0-[rowview]-0-|", options: nil, metrics: nil, views: viewsdictionary)) rowview.superview?.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-0-[rowview]-0-|", options: nil, metrics: nil, views: viewsdictionary))
Comments
Post a Comment