0

ok, I have added a button to the .swift storyboard, I have then added the following code to my ViewController.swift

 @IBOutlet weak var HelloButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    HelloButton.frame.origin.x = view.frame.size.width / 2



}

However when I test it on my iPhone 5s it moves it to the right of my screen and does not make it centered. I am unsure what I need to do to make the button centered.

Yes this is my first helloWorld app, and maybe over my head, but I understand web programing, and I understand to make it center automaticilly I need to have the screen view width total and the button width. I then need to say to the button to divide the screen view width by 2.

for example in CSS we do this

#button{margin:0 auto;}

but this is not HTML or CSS. IT'S SWIFT3

2
  • If you are using a storyboard then you should add constraints in interface builder; center horizontally and center vertically. You can't modify the frame if you are using autolayout.
    – Paulw11
    Sep 22, 2016 at 10:52
  • Try moving to viewWillAppear OR probably even better, viewWillLayoutSubviews. The frames are not always setup and correct during viewDidLoad.
    – Mike
    Sep 22, 2016 at 17:46

4 Answers 4

2

Try this:

    HelloButton.center.x = view.frame.width/2

Yours is not correct because the origin.x is the first point (top left) of the button view, not the center of the view, you can change it's background color to different color to see it

Also, the variable name should be lowerCase, followed by code convention

2
  • this worked for me, and achieved the look I was looking for I combined this code with the one below from @Wilson Balderrama This way it does not matter which way the screen is rotated it will be centered in the middle. I tried to put this code straight into the viewDidLoad area, and while it works it did not auto center when the screen was rotated. The reason this gets the tick, is because it answered the direct question I asked, however for a more detailed answer the two people Tj3n & Wilson's answers need to be combined. Sep 22, 2016 at 23:55
  • If you use autolayout way it will update your view with new layout after you rotate :D It's indeed better than programmatically, this way required you to recall your UI placement again, one tip is that if you put this code to viewDidLayOutSubView instead of viewDidLoad it may automatically update for you
    – Tj3n
    Sep 23, 2016 at 2:49
2

You should be using Autolayout for accomplishing what you want and the way you do is like the following in your ViewController inside of Main.storyboard: (control + left mouse click -> dragging the blue line upwards and then releasing the control and left mouse click, you will see the popup)

In the popup select the options:

  • Center Horizontally In Container
  • Center Vertically in Container

But if you want to do it programmatically, do it in viewWillLayoutSubviews method:

class ViewController: UIViewController {

  @IBOutlet weak var button: UIButton!

  override func viewWillLayoutSubviews() {
    super. viewWillLayoutSubviews()

    button.center = view.center
  }

}
2
  • thank you for providing the override func - have been reading "The Swift Programing Language 2.3 from apple's iBook store and this is not talked about in the book, unless I am looking in the wrong place. Sep 22, 2016 at 23:52
  • you should be calling super's method always, otherwise the provided functionality by the class parent won't be executed.
    – Wilson
    Nov 13, 2017 at 13:27
0

In your project Autolayout is enabled? if Auto Layout enabled then Change UI in viewdidLoad: may not give appropriate result. use -viewWillLayoutSubviews method to do UI task, it you want to center your button

myButton.center = self.view.center 

Hope it will work.

0

Add the code to viewDidAppear that way the actual view has shown up. Also, you should use button.center.x instead of button.frame.origin.x if you want to center it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.