0

Background

I’m trying to create a UIButton with a 300px width and 200px height.

Then, I’m trying to position that UIButton centred horizontally and 50 pixels from the bottom.

When running the code in the iOS simulator, the result is unexpected, the button height and width is not correct, the UIButton appears cropped. Image below.


Question

What corrections to the code below must be made so that the UIButton layout is positioned correctly and retains the correct sized UIButton frame?


Code

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addButton1()
    }

    func addButton1() {
        let myButton1 = UIButton(type: UIButton.ButtonType.custom)
        myButton1.frame.size = CGSize(width: 300, height: 200)
        myButton1.setTitle("Hello", for:UIControl.State())
        myButton1.backgroundColor =  .blue
        view.addSubview(myButton1)
        myButton1.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            myButton1.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            myButton1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50)
        ])
    }

}

Image

enter image description here

3 Answers 3

2

You are mixing legacy frame approach with AutoLayout and that would not work. I suggest to stick with AutoLayout and so your code could be:

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()

    addButton1()
  }

  func addButton1() {
    let myButton1 = UIButton(type: UIButton.ButtonType.custom)
    myButton1.setTitle("Hello", for:UIControl.State())
    myButton1.backgroundColor =  .blue
    view.addSubview(myButton1)
    myButton1.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      myButton1.widthAnchor.constraint(equalToConstant: 300),
      myButton1.heightAnchor.constraint(equalToConstant: 200),
      myButton1.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      myButton1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50)
    ])
  }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you very much for explaining the issue.
1

You can give the height and width of the button through NSLayoutConstraint.

func addButton1() {
        let myButton1 = UIButton(type: UIButton.ButtonType.custom)
        myButton1.setTitle("Hello", for:UIControl.State())
        myButton1.backgroundColor =  .blue
        view.addSubview(myButton1)
        myButton1.translatesAutoresizingMaskIntoConstraints = false
        myButton1.widthAnchor.constraint(equalToConstant: 300).isActive = true
        myButton1.heightAnchor.constraint(equalToConstant: 200).isActive = true
        myButton1.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        myButton1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
    }

Comments

1

As my knowledge, I think we could try with Aspect ratio.

For example:

button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/4.0).isActive = true

button.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
button.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 16.0).isActive = true
button.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: -16.0).isActive = true
button.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: -20.0).isActive = true
button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/4.0).isActive = true

If you have a time please reading carefully a apple document for autolayout

2 Comments

The heightAnchor didn't display as expected as the multiplier needed to be 200/300. After changing that it worked. Interesting approach. Thank you.
Yeah! Happy to hear that. Actually, in case I think we should use aspect ratio because it helps the button fit with all iphone version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.