Reputation: 477
I'm trying to make this label appear in random areas of the screen at random sizes when a button is pressed.
Any guidance is appreciated.
Here's what I have so far:
@IBAction func Button(sender: AnyObject) {
//random x and 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)
}
Upvotes: 0
Views: 95
Reputation: 131481
You are probably using AutoLayout. With AutoLayout you can't change a view's frame and have it work. Often the constraints override the changes you make and nothing happens.
Instead, do this:
Create a horizontal position constraint (based on the leading edge) and a vertical position constraint (based on the top layout guide.)
Create height and width constraints.
Control-drag from all 4 constraints into the top of your view controller's code to create outlets for your constraints.
Then simply apply your changes to the constant property of your constraints and call layoutIfNeeded on the button's superview (or on the view controller's content view - that should work too.)
Upvotes: 1