swift - Updating existing constraints does not work, wrong use of .active property? -
i have storyboard contains :
- a "tab bar" on left 5 tabs
- a container on right side of tab bar contains 6 image views share same space looks :
each image view configured occupy 1/3 of container's width , 1/2 of height.
however, different ratios can provided @ runtime (from json file) example, 1st image view's height become 70% of container's height , 50% of width (therefore, 2nd , 3rd image views widths occupy 25% of container's width , 4th image view, 2nd line column 1 has height of 30% of image view).
to here tried :
-create 2 arrays of outlets (width , height constraints image views) :
// height constraints @iboutlet var heightconstraints: [nslayoutconstraint]! // width constraints @iboutlet var widthconstraints: [nslayoutconstraint]!
-create outlet container of image views
// drawings - container @iboutlet weak var drawingview: uiview!
-create stored properties update constraints (these not outlets)
// drawing height property var drawingheightconstraint: nslayoutconstraint? // drawing width property var drawingwidthconstraint: nslayoutconstraint?
and here comes job : i've overridden updateviewconstraints(), mistake ? seemed best place update constraint saw people use viewwillappear... method called every time click on tab (and load new drawings new ratios)
public override func updateviewconstraints() { super.updateviewconstraints() // activate default height , width constraints papoohomepageviewcontroller.activateconstraint(constraints: heightconstraints) papoohomepageviewcontroller.activateconstraint(constraints: widthconstraints) // deactivate temporarily created new height , width constraints drawingheightconstraint?.active = false drawingwidthconstraint?.active = false // display drawings let drawingelements:[(string, double, double)] = papoobrain.getdrawings(forsection: currentsection) (index, (drawing, heightratio, widthratio)) in drawingelements.enumerate() { drawingviews[index].image = uiimage(named: drawing) // update height constraint if ratio different defaut ratio of 1/2 if heightratio != double(heightconstraints[index].multiplier) { heightconstraints[index].active = false drawingheightconstraint = nslayoutconstraint(item: drawingviews[index], attribute: .height, relatedby: .equal, toitem: drawingview, attribute: .height, multiplier: cgfloat(heightratio), constant: 0) drawingheightconstraint!.active = true } if widthratio != double(widthconstraints[index].multiplier) { widthconstraints[index].active = false drawingwidthconstraint = nslayoutconstraint(item: drawingviews[index], attribute: .width, relatedby: .equal, toitem: drawingview, attribute: .width, multiplier: cgfloat(widthratio), constant: 0) drawingwidthconstraint!.active = true } } // 1 should or should not call, in order ? drawingview.setneedsupdateconstraints() drawingview.setneedslayout() drawingview.layoutifneeded() }
-here code of little helper activate constraints. note: problem. try activate constraint deactivated (it comes array of outlets) don't want duplicated
class func activateconstraint(constraints constraints: [nslayoutconstraint]) { constraint in constraints { constraint.active = true } }
-finally, here piece of json see parse...
"drawings": [ { "image": "01-01-drawing.png", "height-ratio": "0.5", "width-ratio": "0.33", etc.
the problem(s)
- if change configuration file (json) "okay, image view 1's height ratio o.7 , image view 4's height ratio 0.3" : have conflicting constraints (it seems after all, "active" property did not deactivate constraint
so when debugging, see constraints of width , height getting duplicated, causing nightmare.
- the same happens height etc.
many questions here
did use correctly (in terms of lifecycle etc.) updateviewconstraints() ? call super @ beginning ?
what correct use of setneedslayout, layoutifneeded, setneedsupdateconstraints...
when set outlet nslayoutconstraint's active property false. active = true afterwards, did correct reference default height/width storyboard ?
thank reading
Comments
Post a Comment