Swift iOS -How to set UIView's Height Anchor <= To A Label's Intrinsic Text Size? 'NSLayoutConstraint' is not convertible to 'Bool' -
i have programmatic view label inside of i'm pinning bottom of navbar. there dynamic text inside of label , want view label in @ least 64 pts or bigger if height of text makes smaller.
the intrinsic size of text label sets view @ noticeable height.
setviewandlabel(dynamictext: "unknown error\nplease try request again\error: 123")
however intrinsic size of text makes height small:
setviewandlabel(dynamictext: "message deleted!")
the message deleted! should more along lines of:
i used return keys set don't think that's correct way go because different messages generated:
setviewandlabel(dynamictext: "\nmessage deleted!\n")
i tried:
if myview.heightanchor.constraint(lessthanorequaltoconstant: 64){ myview.heightanchor.constraint(equaltoconstant: 64).isactive = true }
but error:
'nslayoutconstraint' not convertible 'bool'
what's best way set height view label in minimum height?
var mylabel: uilabel(){ let label = uilabel() label.translatesautoresizingmaskintoconstraints = false label.textcolor = uicolor.white label.font = uifont(name: "helvetica-regular", size: 19) label.numberoflines = 0 label.sizetofit() label.textalignment = .center return label } let myview:uiview = { let view = uiview() view.translatesautoresizingmaskintoconstraints = false return view }() override func viewwillappear(_ animated: bool) super.viewwillappear(animated){ setviewandlabel(dynamictext: //some text set here) } func setviewandlabel(dynamictext: string){ view.addsubview(myview) myview.backgroundcolor = uicolor.red myview.topanchor.constraint(equalto: view.topanchor, constant: 64).isactive = true view.widthanchor.constraint(equalto: view.widthanchor, constant: 0).isactive = true myview.addsubview(mylabel) mylabel.text = dynamictext mylabel.topanchor.constraint(equalto: myview.topanchor, constant: 0).isactive = true mylabel.widthanchor.constraint(equalto: myview.widthanchor, constant: 0).isactive = true myview.bottomanchor.constraint(equalto: mylabel.bottomanchor, constant: 0).isactive = true //this if statement doesn't work if myview.heightanchor.constraint(lessthanorequaltoconstant: 64){ viewforerrorlabel.heightanchor.constraint(equaltoconstant: 64).isactive = true } }
this how have set constraints:
view.addsubview(myview) myview.topanchor.constraint(equalto: toplayoutguide.bottomanchor).isactive = true myview.leadinganchor.constraint(equalto: view.leadinganchor).isactive = true myview.trailinganchor.constraint(equalto: view.trailinganchor).isactive = true myview.heightanchor.constraint(greaterthanorequaltoconstant: 64).isactive = true myview.addsubview(mylabel) mylabel.topanchor.constraint(equalto: myview.topanchor).isactive = true mylabel.leadinganchor.constraint(equalto: myview.leadinganchor).isactive = true mylabel.trailinganchor.constraint(equalto: myview.trailinganchor).isactive = true mylabel.bottomanchor.constraint(equalto: myview.bottomanchor).isactive = true
you not need check label's height @ all. can create height greater or equal constraint myview
, height never smaller 64pt (or whatever value set to) - if label contains short text.
Comments
Post a Comment