ios - changing layout anchors of view on an action -
i have 1 upperlabel, 1 view , button.
in viewdidload()
method have given layout constraints view. on tap of button need update constraints(i.e view should move downwards). how can that.
here code that. not working.
let keyview = uiview() override func viewdidload() { keyview.frame = cgrect(x: 0, y: 0, width: 100, height: 30) keyview.backgroundcolor = uicolor.cyan keyview.translatesautoresizingmaskintoconstraints = false containerview.addsubview(keyview) keyview.topanchor.constraint(equalto: upperlabel.bottomanchor , constant: 10).isactive = true keyview.leadinganchor.constraint(equalto: containerview.leadinganchor, constant: 20).isactive = true keyview.trailinganchor.constraint(equalto: containerview.trailinganchor, constant: -20).isactive = true keyview.bottomanchor.constraint(equalto: keyview.topanchor, constant: keyview.frame.size.height).isactive = true }
code in action follows.
func buttonaction(sender: uibutton!) { keyview.translatesautoresizingmaskintoconstraints = false keyview.topanchor.constraint(equalto: upperlabel.bottomanchor, constant: cgfloat(i*50)) keyview.leadinganchor.constraint(equalto: containerview.leadinganchor, constant: 20) keyview.trailinganchor.constraint(equalto: containerview.trailinganchor, constant: -20) keyview.bottomanchor.constraint(equalto: keyview.topanchor, constant: keyview.frame.size.height) + =1 }
as per above code keyview should move downwards on clicking of button. not moving down.
please me out. in advance.
take global variable of type nslayoutconstraint follows..
var keyviewtopconstraint: nslayoutconstraint?
now in viewdidload while giving initial constraints. give follows
keyviewtopconstraint = keyview.topanchor.constraint(equalto: upperlabel.bottomanchor , constant: 10) keyviewtopconstraint?.isactive = true
now in action. remove initial constraint , create constraint , make active. follows
keyviewtopconstraint?.isactive = false keyviewtopconstraint = keyview.topanchor.constraint(equalto: upperlabel.bottomanchor, constant: cgfloat(i*50)) keyviewtopconstraint?.isactive = true
Comments
Post a Comment