Change width & height at UIView()

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

Answered by Bruno Developer in 626682022
I solved for my self. If help the somebody with the same doubt, I made this solution below

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!!


Accepted Answer
I solved for my self. If help the somebody with the same doubt, I made this solution below

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!!


Why do you do it in code and not in IB ? Setting width and height is immediate there.
Hello! I think did u mean IBOutlets? I can't, because my on the project our team are using only View Code without Storyboards or xibs. By the way, Storyboards is a great way too. I was working with that on the last project.

Cheers!
Change width & height at UIView()
 
 
Q