Coder221
Coder221

Reputation: 1433

Adding button to view created programatically

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.

Upvotes: 0

Views: 97

Answers (3)

rs7
rs7

Reputation: 1630

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

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

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)

Upvotes: 1

Lê Công
Lê Công

Reputation: 21

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)

Upvotes: 0

Related Questions