Hussein Reda AlBehary
Hussein Reda AlBehary

Reputation: 115

How to center UIButton in Swift?

I have this code for my UIButton. What should I change in my CGRectMake to set my UIButton in the center of the screen for all screen sizes?

let loginBtn = UIButton(frame: CGRectMake(60, 360, 240, 40))
loginBtn.layer.borderColor = UIColor.whiteColor().CGColor
loginBtn.layer.borderWidth = 2
loginBtn.titleLabel!.font = UIFont.systemFontOfSize(24)
loginBtn.tintColor = UIColor.whiteColor()
loginBtn.setTitle("Login", forState: UIControlState.Normal)
self.view.addSubview(loginBtn)

Upvotes: 0

Views: 20991

Answers (6)

Ethan Bonin
Ethan Bonin

Reputation: 1

I found this to be the best solution for me..

Swift 4

myButton.center.x = self.view.frame.midX 
myButton.center.y = self.view.frame.midY

I found if I need to center things quite often, I usually use a generic solution.

extension UIViewController {
    func centerComponent(_ component: AnyObject) {
        let customView = component as! UIView
        customView.center.x = self.view.frame.midX
        customView.center.y = self.view.frame.midY
    }
}

then you can call it from any UIViewcontroller inside a function like so:

centerComponent(myButton)

Upvotes: 0

Iryna Batvina
Iryna Batvina

Reputation: 1684

For Swift 4

@IBOutlet weak var btStart: UIButton!

on the middle of any screen (any device)

btStart.center = self.view.center

OR

btStart.center.x = self.view.center.x
btStart.center.y = self.view.center.y

on the center by x and 25 percent from top

btStart.center.x = self.view.center.x
btStart.center.y = self.view.center.y / 2

Upvotes: 0

Daniel Storm
Daniel Storm

Reputation: 18878

Set your UIButton's center to the center of the view it is in.

loginBtn.center = view.center

Upvotes: 3

mrunal thanki
mrunal thanki

Reputation: 751

For your place uibutton center of your view , update your cgrectmake as bleow..

CGRectMake((self.view.frame.size.width - 240) / 2, (self.view.frame.size.height - 40) / 2,240,40)  

or

You can add one line after your code

loginBtn.center = self.view.center

For SignUp Button :  

signup.frame = loginBtn.bounds
signup.center = CGPointMake(loginBtn.center.x, loginBtn.center.y + loginBtn.frame.size.height + 10)

Upvotes: 9

Marcus Rossel
Marcus Rossel

Reputation: 3258

This will center the button across the whole screen, not just the view it's in:

let verticalCenter: CGFloat = UIScreen.mainScreen().bounds.size.height / 2.0
let horizontalCenter: CGFloat = UIScreen.mainScreen().bounds.size.width / 2.0

loginBtn.center = CGPoint(x: horizontalCenter, y: verticalCenter)

Edit:

As @LeoDabus pointed out, this can be compacted by using the midX and midY properties on CGRect:

let verticalCenter: CGFloat = UIScreen.mainScreen().bounds.midY
let horizontalCenter: CGFloat = UIScreen.mainScreen().bounds.midX

Upvotes: 1

Amit Raj
Amit Raj

Reputation: 1378

You need add the following line prior to self.view.addSubview(loginBtn).

loginBtn.center = self.view.center

Upvotes: 1

Related Questions