ios - UITableViewCell with UITextfield not centeringY in correct manner -
i want tableviewcells vertically centered uitextfield. think set nslayoutconstraint right, textfield not centered. did wrong?

func createcellaseditablecell(){ let textfield = uitextfield(frame: cgrectmake(0, 0, self.contentview.frame.width, self.contentview.frame.height)) let views = ["textfield" : textfield] self.contentview.addsubview(textfield) let horizontalconstraint = nslayoutconstraint.constraintswithvisualformat("h:|-20-[textfield]", options: nslayoutformatoptions(0), metrics: nil, views: views) let verticalconstraint = nslayoutconstraint(item: textfield, attribute: nslayoutattribute.centery, relatedby: nslayoutrelation.equal, toitem: self.contentview, attribute: nslayoutattribute.centery, multiplier: 1, constant: 0) textfield.settranslatesautoresizingmaskintoconstraints(false) self.contentview.settranslatesautoresizingmaskintoconstraints(false) nslayoutconstraint.activateconstraints(horizontalconstraint) nslayoutconstraint.activateconstraints([verticalconstraint]) textfield.placeholder = "textfield" }
the problem see here mixing setting frame code , setting constraints @ same time. not want have auto resizing mask translated constraints, position textfield in container making big fit contentview rectangle according positioning constraints (look @ width of textfield , margin set it).
what is:
1) create textfield without setting frame code (use different initialiser -> in objective-c [[uitextfield alloc]init])
2) leave centre constraints are
3) set vertical constraint v:|[textfield]|
4) set horizontal 1 h:|-20-[textfield]|
5) not turn off translationautoresizingmask on contentview, you not responsible layouting in container wrong!
Comments
Post a Comment