ios - Set X and Y values of UILabel using NSLayout -
i adding ui label , trying out programmatic ui code. set label using code:
let titlelabel: uilabel = { let lb = uilabel() lb.translatesautoresizingmaskintoconstraints = false lb.textalignment = .center lb.numberoflines = 1 lb.textcolor = uicolor.black lb.font=uifont.systemfont(ofsize: 22) lb.backgroundcolor = uicolor.white return lb
and added constraints using code:
func setuptitlelabel() { titlelabel.translatesautoresizingmaskintoconstraints = false titlelabel.heightanchor.constraint(equaltoconstant: 100).isactive = true titlelabel.widthanchor.constraint(equaltoconstant: 200).isactive = true titlelabel.centerxanchor.constraint(equalto: titlelabel.superview!.centerxanchor).isactive = true titlelabel.centeryanchor.constraint(equalto: titlelabel.superview!.centeryanchor).isactive = true }
i call function later on. however, not sure how change x , y values. currently, set middle of page both x , y, how move top.
as said in comment, using titlelabel.superview!.centeryanchor
wrongly if need pin label top of label superview need use titlelabel.superview!.topanchor
instead
replace this
titlelabel.centeryanchor.constraint(equalto: titlelabel.superview!.centeryanchor).isactive = true
by this
titlelabel.centeryanchor.constraint(equalto: titlelabel.superview!.topanchor).isactive = true
wiki
Comments
Post a Comment