Reputation: 60301
I have my UILabel
code as below. It works find showing UILabel in my ViewController.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myText = UILabel()
myText.text = "Testing"
view.addSubview(myText)
myText.translatesAutoresizingMaskIntoConstraints = false
myText.backgroundColor = .green
NSLayoutConstraint.activate([
myText.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myText.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
However if I change to UITextView
as below, nothing appears. What did I do wrong?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myText = UITextView()
myText.text = "Testing"
view.addSubview(myText)
myText.translatesAutoresizingMaskIntoConstraints = false
myText.backgroundColor = .green
NSLayoutConstraint.activate([
myText.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myText.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
Upvotes: 0
Views: 49
Reputation: 559
you have to give height and width
to UITextView
or give Top, Bottom, Left, Right anchor
.
NSLayoutConstraint.activate([
myText.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myText.centerYAnchor.constraint(equalTo: view.centerYAnchor),
myText.heightAnchor.constraint(equalToConstant: "Your height"),
myText.widthAnchor.constraint(equalToConstant: "Your width")
])
Thanks
Upvotes: 2