Using variables in CGRectMake, Swift, UIkit Using variables in CGRectMake, Swift, UIkit xcode xcode

Using variables in CGRectMake, Swift, UIkit


Swift 3 update

let rect = CGRect(x: 0, y: 0, width: 100, height: 100)


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 = 100Label1.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...


So, there is many ways to skin the cat. It all depends what your needs and requirements are (maybe you could elaborate a bit on what you are trying to achieve?). But one way to do it could be to set a variable when something happens, and then update the frame of the label. If you added a tap gesture recognizer to your view, and updated your label like so:

let myLabel = UILabel()override func viewDidLoad() {    super.viewDidLoad()    let tapGestRecog = UITapGestureRecognizer(target: self, action: "handleTap:")    self.view.addGestureRecognizer(tapGestRecog)}func handleTap(sender:UIGestureRecognizer) {    let newXposition = sender.locationInView(self.view).x    let newYposition = sender.locationInView(self.view).y    myLabel.frame = CGRectMake(newXposition, newYposition, 200, 200)}

This is just an example, and a very crude way of doing it. There are many other ways of doing it, but it hopefully gives you an idea of how to achieve it.