I am trying to place a UISquare() object, square
, within its parent view, squareParentView
. When I try to achieve this using auto layout, I fail miserably.
Here is my UISquare class for reference:
class UISquare: UIView() {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
//.. setup the square. Implementation details are not relevant to this question ..
}
}
Here is my ViewController class:
class viewController: UIViewController {
let square = UISquare(x: 100, y: 100, width: 100, height: 100)
let squareParentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(squareParentView)
squareParentView.translatesAutoresizingMaskIntoConstraints = false
// Here I am simply making squareParentView the same size as self.view
squareParentView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
squareParentView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
squareParentView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
squareParentView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
// Here I am setting up the square and enabling auto layout
squareParentView.addSubview(square)
square.translatesAutoresizingMaskIntoConstraints = false
// PROBLEM AREA RIGHT HERE
square.centerXAnchor.constraint(equalTo: squareParentView.centerXAnchor).isActive = true
square.centerYAnchor.constraint(equalTo: squareParentView.centerYAnchor).isActive = true
}
}
When I execute the code, the square is simply not appearing on the screen. It's like it completely ignored auto layout and my instructions to have its center in its parent view's center.
If I get rid of the lines where I set the square's constraints, (i.e. if I don't use auto layout at all), the square appears at the top left corner of squareParentView, as expected.
I suspect that the reason why this isn't working is because when you deal with UIViews (such as UISquare), it is not enough to simply change the layout constraints, but rather the UIView's frame should be set to the desired position (i.e. in my case, set square.center = squareParentView.center
). However, when I set it, there is no observable difference. In any case, I don't want to mix up auto layout with frames in my codebase, so I would like to know if there is a way to center this square in its parent view using auto layout exclusively.
I also have a follow up question:
A print statement in my code reveals something rather strange:
print(squareParentView.center) // prints (0,0)
Why is my parent view's center set to (0,0) when I set its constraints programmatically so that it would fill up self.view
?
i.e.
squareParentView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
squareParentView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
squareParentView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
squareParentView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
What gives?