HeeysamH HeeysamH - 3 months ago 26
Swift Question

Swift CGRectMake Center button

I'm programming a button to my

swift
spriest project.
but the location of the button is messing up.
I wan it to be in the middel but it goes some random place.

let button = UIButton(type: .System) // let preferred over var here
button.frame = CGRectMake(self.frame.width / 2, self.frame.height / 2, 100, 50)
print(self.frame.height / 2)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Button", forState: UIControlState.Normal)
self.view!.addSubview(button)


I don't know why it does it with a
CGPoint
it works but i can't use a
CGPoint
here.

Answer

Try this...

button.frame = CGRectMake(
    self.view.center.x - (WidthOfButton / 2), 
    self.view.center.y - (HeightOfButton / 2), 
    WidthOfButton, 
    HeightOfButton)