Mats Mats - 5 months ago 104
iOS Question

Using variables in CGRectMake, Swift, UIkit

For an App I'm making i need to use variables to change the size and position of objects (Labels). I've tried

var example = CGRectMake(0, 0, 0, 100)
, hoping it would ignore the zeros (Not really thinking it would though). I then tried:

var example = 100
Label1.frame = CGRectMake(20, 20, 50, example)


I changed the syntax a bit, adding "" and replacing the
CGRectMake
with
CGRect
etc, but nothing worked... I don't get what I'm doing wrong here... Help!

Answer

CGRectMake takes CGFloats for all of its arguments. Your sample code should work fine if you specify that example is supposed to be a CGFloat, using a type identifier:

         //  v~~~~ add this...
var example: CGFloat = 100
Label1.frame = CGRectMake(20, 20, 50, example)

Otherwise, swift infers the type of example to be Int, and the call to CGRectMake fails, cuz it can't take an Int as a parameter...