I've created a UIView
programmatically using layout anchors
. Now I want to add a UILabel
inside this view. Here is my code so far :
let centerView = UIView()
centerView.translatesAutoresizingMaskIntoConstraints = false
centerView.backgroundColor = UIColor.white
view.addSubview(centerView)
centerView.leftAnchor.constraint(equalTo: view.leftAnchor, constraint: 20).isActive = true
centerView.rightAnchor.constraint(equalTo: view.rightAnchor, constraint: -20).isActive = true
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Testing"
label.textColor = UIColor.black
centerView.addSubview(label)
label.leftAnchor.constraint(equalTo: centerView.leftAnchor).isActive = true
I thought this label would be displayed in reference to centerView
but it is rather being displayed in reference to the UIWindow
. This is the current view hierarchy :
UIWindow --> UIView (centerView) --> UILabel (label)
I need to add multiple labels inside centerView
and as per my understanding, this chain will get longer whereas I want several labels to be all under centerView
UIWindow
|
UIView (centerView)
/ | \
Label 1 Label 2 Label 3
How can I achieve this type of hierarchy?