0

I have a view that is created programatically as follows

  let viewOne = UIView()
        viewOne.contentMode = .scaleAspectFit
        viewOne.backgroundColor = UIColor.blue
        viewOne.layer.masksToBounds = true
        viewOne.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(viewOne)

    NSLayoutConstraint.activate([
        viewOne.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        viewOne.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        viewOne.heightAnchor.constraint(equalToConstant:200),
        viewOne.widthAnchor.constraint(equalToConstant:200)])

I have the following button that is created programmatically as follows,

    let button = UIButton()
    button.frame = CGRect(x: 125, y: 125, width: 100, height: 50)
    button.backgroundColor = UIColor.green
    button.setTitle("Ok", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    viewOne.addSubview(button)

How do I add the button to the center of viewOne. Thanks in advance.

4
  • It would be the same as the code you posted to put viewOne in the center. Just change the views referenced in the constraints. Commented Jun 20, 2019 at 16:12
  • Ok, but I don't get what is button.frame for? Commented Jun 20, 2019 at 16:13
  • it's better not to mix constraints with frames Commented Jun 20, 2019 at 16:15
  • Ok, thanks for clarification. Commented Jun 20, 2019 at 16:16

3 Answers 3

1

You can try

let viewOne = UIView()
viewOne.contentMode = .scaleAspectFit
viewOne.backgroundColor = UIColor.blue
viewOne.layer.masksToBounds = true
viewOne.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(viewOne)

let button = UIButton() 
button.backgroundColor = UIColor.green
button.setTitle("Ok", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 
button.translatesAutoresizingMaskIntoConstraints = false
viewOne.addSubview(button)

NSLayoutConstraint.activate([

    viewOne.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    viewOne.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    viewOne.heightAnchor.constraint(equalToConstant:200),
    viewOne.widthAnchor.constraint(equalToConstant:200),

    button.centerXAnchor.constraint(equalTo: viewOne.centerXAnchor),
    button.centerYAnchor.constraint(equalTo: viewOne.centerYAnchor),  
    button.heightAnchor.constraint(equalToConstant:100),
    button.widthAnchor.constraint(equalToConstant:50)

])

Tip:

you can optionally remove these 2 constraints as the button has intrinsic content size by default but if you need a fixed content leave them

 button.heightAnchor.constraint(equalToConstant:100),
 button.widthAnchor.constraint(equalToConstant:50)
Sign up to request clarification or add additional context in comments.

Comments

0

Button it self has the width and height. So you do not have to fix it

button.centerXAnchor.constraint(equalTo: viewOne.centerXAnchor)
button.centerYAnchor.constraint(equalTo: viewOne.centerYAnchor)

Comments

0

You can either use autolayout, as shown below:

    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = UIColor.green
    button.setTitle("Ok", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    viewOne.addSubview(button)
    NSLayoutConstraint.activate([
    button.centerXAnchor.constraint(equalTo: viewOne.centerXAnchor),
    button.centerYAnchor.constraint(equalTo: viewOne.centerYAnchor),
    button.heightAnchor.constraint(equalToConstant:50),
    button.widthAnchor.constraint(equalToConstant:100)])

Or place your button manually, after doing some calculation.

let button = UIButton(frame: CGRect(x: 50, y: 75, width: 100, height: 50))
viewOne.addSubview(button)

I obtained x and y as follows: x = viewOne width/2 - button width/2 = 200/2 - 100/2 = 50 y = viewOne height/2 - button height/2 = 200/2 - 50/2 = 75

Comments

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.