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.
viewOnein the center. Just change the views referenced in the constraints.