ios - Why these layout issues occur when debugging view hierarchy while the UI layout in simulator is OK -
i use visual format layout subviews in cell this:
contentview.addsubview(accountlabel) contentview.addsubview(contentlabel) contentview.addsubview(timelabel) let views: [string : any] = [ "accountlabel": accountlabel, "contentlabel": contentlabel, "timelabel": timelabel ] let hconstraint1 = nslayoutconstraint.constraints(withvisualformat: "h:|-15-[accountlabel]-[timelabel]-8-|", options: [.alignallcentery], metrics: nil, views: views) let hconstraint2 = nslayoutconstraint.constraints(withvisualformat: "h:[contentlabel]-8-|", options: [], metrics: nil, views: views) let vconstraint = nslayoutconstraint.constraints(withvisualformat: "v:|-12-[accountlabel]-8-[contentlabel]-12-|", options: [.alignallleading], metrics: nil, views: views) nslayoutconstraint.activate(hconstraint1+hconstraint2+vconstraint)
and looks ok when run in simulator:
however, layout issues occur when debugging view hierarchy:
why these layout issues occur while runtime ui looks ok?
is wrong visual format string above? or it's bug of xcode?
how remove these layout issues?
well, i'm not expert constraints, see 1 "correctable" issue , 1 "that's weird maybe it's bug" issue.
as understand it, auto-layout try satisfy constraints -- even if ambiguous. if fails, see error message in console, along lines of "will attempt satisfy breaking..."
so warning information see in debug view hierarchy
more informational "fix error" directive.
that said...
you have accountlabel
, timelabel
constrained relative each other, both have same compression resistance
. so, long amount of text in each label "fit", ok.
but suppose accountlabel
gets text string of "this user name long fit here"?
should (i use background colors see frames):
or this:
that's why horizontal position , width showing "ambiguous".
you can fix issue adding 1 line. assuming want accountlabel
truncated:
accountlabel.setcontentcompressionresistancepriority(749, for: .horizontal)
will trick. default 750
, line tells auto-layout give timelabel
higher resistance.
the height , vertical positions showing ambiguous don't seem right. don't see wrong constraints.
if use same font 3 labels? no ambiguous warnings.
if use different font size timelabel
(as show in images), there are warnings in debug view hierarchy.
except... if scroll cells reused? warnings have gone away!
edit: op states in comment, vertical ambiguity can eliminated (prior scrolling) giving different content hugging priority
values accountlabel
, contentlabel
.
since default hugging
priority 250
:
accountlabel.setcontenthuggingpriority(249, for: .vertical)
clears warning.
i'm not convinced it's necessary... does appear job.
Comments
Post a Comment