ios - Show a label in random places on the screen -
i'm trying make label appear in random areas of screen @ random sizes when button pressed.
any guidance appreciated.
here's have far:
@ibaction func button(sender: anyobject) { //random x , y value var xvalue = cgfloat(arc4random() % 50) var yvalue = cgfloat(arc4random() % 20) //random sizes var xsize = cgfloat(arc4random_uniform(150) + 60) var ysize = cgfloat(arc4random_uniform(150) + 60) label.text = faces.randomface() label.frame = cgrectmake(xvalue, yvalue, xsize, ysize) }
you using autolayout. autolayout can't change view's frame , have work. constraints override changes make , nothing happens.
instead, this:
create horizontal position constraint (based on leading edge) , vertical position constraint (based on top layout guide.)
create height , width constraints.
control-drag 4 constraints top of view controller's code create outlets constraints.
then apply changes constant property of constraints , call layoutifneeded on button's superview (or on view controller's content view - should work too.)
Comments
Post a Comment