Code Block
Hello from NY o/
On the many projects, I was working out with Storyboards.
From now on, I'm beginning with View Code and I'm trying to change the size of the UIView as something like that: e.g
Code Block class ViewController: UIViewController { // MARK: - Subviews private lazy var containerView: UIView = { let view = UIView() view.backgroundColor = UIColor.green view.frame.size.height = 200.0 view.frame.size.width = 300.0 view.sizeToFit() view.translatesAutoresizingMaskIntoConstraints = false return view }() // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.white view.addSubview(containerView) containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true } }
And the size of the View is not sizing. I need use CGRect to change the size of the UIView? But how to center with auto layout when CGRect(x: 0, y: 0, height: 200, width: 350)?
The x & y stay to hard to keep center x & center y at any screen size?
Thanks a lot for you help.
Cheers.
Code Block
I solved for my self. If help the somebody with the same doubt, I made this solution below
... viewDidLoad() ...
That's possible to change the size of the view with CGRect or using with NSLayout approaching.
https://developer.apple.com/documentation/uikit/view_layout
Cheers!!
Code Block private lazy var containerView: UIView = { let view = UIView() view.backgroundColor = UIColor.black view.translatesAutoresizingMaskIntoConstraints = false return view }()
... viewDidLoad() ...
Code Block view.addSubview(containerView) containerView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 0).isActive = true containerView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -120).isActive = true containerView.widthAnchor.constraint(equalToConstant: 250).isActive = true containerView.heightAnchor.constraint(equalToConstant: 125).isActive = true
That's possible to change the size of the view with CGRect or using with NSLayout approaching.
https://developer.apple.com/documentation/uikit/view_layout
Cheers!!