Add constraint to programmatically created button in swift 1.2 -


i using code create button in swift. works great except not know how add constraints code.

    let button   = uibutton.buttonwithtype(uibuttontype.system) uibutton button.frame = cgrectmake(100, 100, 100, 50) button.backgroundcolor = uicolor.greencolor() button.settitle("test button", forstate: uicontrolstate.normal) button.addtarget(self, action: "buttonaction:", forcontrolevents: uicontrolevents.touchupinside)  self.view.addsubview(button) 

i place button 50 trailing , 100 top. appreciated.

try that:

    ....     self.view.addsubview(button)      button.settranslatesautoresizingmaskintoconstraints(false)      let views = ["button": button]      var constrainthorizontal = nslayoutconstraint.constraintswithvisualformat("h:|-50-[button]", options: nslayoutformatoptions(0), metrics: nil, views: views)     view.addconstraints(constrainthorizontal)     var constraintwertical = nslayoutconstraint.constraintswithvisualformat("v:|-100-[button]", options: nslayoutformatoptions(0), metrics: nil, views: views)     view.addconstraints(constraintwertical) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -