swift - UIButton changing frame when constraint added -
i have circular uibutton inside uitableviewcell i'm creating programmatically. works fine, until add constraint center in it's superview.
this button looks before add constraint it:

and looks after add constraint:

here's code:
var thumbnailbtn = uibutton.buttonwithtype(.custom) as! uibutton func setupviews() { backgroundcolor = .colorfromcode(0x3f3f3f) thumbnailbtn.frame = cgrectmake(0, 0, 80, 80) thumbnailbtn.layer.cornerradius = 0.5 * thumbnailbtn.bounds.size.width thumbnailbtn.backgroundcolor = uicolor.greencolor() thumbnailbtn.settitle("test", forstate: uicontrolstate.normal) thumbnailbtn.addtarget(self, action: "buttonaction:", forcontrolevents: uicontrolevents.touchupinside) contentview.addsubview(menucontrolcontainerview) menucontrolcontainerview.addsubview(thumbnailbtn) updateconstraints() } override func updateconstraints() { if !didsetupconstraints { uiview.autosetpriority(1000) { self.menucontrolcontainerview.autosetcontentcompressionresistancepriorityforaxis(.vertical) } menucontrolcontainerview.autopinedgetosuperviewedge(.top, withinset: 0) menucontrolcontainerview.autopinedgetosuperviewedge(.leading, withinset: 0) menucontrolcontainerview.autopinedgetosuperviewedge(.trailing, withinset: 0) menucontrolcontainerview.autopinedgetosuperviewedge(.bottom, withinset: 0) thumbnailbtn.autocenterinsuperview() didsetupconstraints = true } super.updateconstraints() } i noticed if add line of code [ thumbnailbtn.autosetdimensionstosize(cgsizemake(80, 80)) ] updateconstraints() works , looks this:

but cleanest way since i'm setting size twice?
any appreciated. thanks.
you set size in initial frame inappropriate when later start using constraints specify position (and superview layout). should create plain view (using appropriate method 3rd party library you're using) , specify size , position using constraints (the ones you've tried appropriate).
the view resized when centre because 3rd party library removes auto-resizing mask conversion constraints, allows constraint engine whatever wants settings satisfy equations.
Comments
Post a Comment