I have an iOS project I'm working on using Xcode 9.2 and Swift4. I have a UITextView but second the UITextView not visible.
let logo: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "logoyeni"));
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let bannerText: UITextView = {
let textView = UITextView()
textView.text = "Müziğin Sosyal Medyası";
textView.textColor = UIColor.salmon;
textView.textAlignment = .center
textView.isEditable = false
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isScrollEnabled = false
return textView
}()
let slogan: UITextView = {
let textView1 = UITextView()
textView1.text = "Lorem Ipsum Dolor Sit Amet Consectetur";
//textView.font = UIFont.textStyle3;
//textView.textColor = UIColor.cloudyBlue;
textView1.textAlignment = .center;
textView1.isEditable = false
textView1.translatesAutoresizingMaskIntoConstraints = false;
return textView1
}()
ViewDidLoad =
super.viewDidLoad()
view.addSubview(logo);
view.addSubview(bannerText);
view.addSubview(slogan);
setup();
and constrains =
private func setup(){
logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
logo.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
logo.widthAnchor.constraint(equalToConstant: 127).isActive = true
logo.heightAnchor.constraint(equalToConstant: 127).isActive = true
bannerText.topAnchor.constraint(equalTo: logo.bottomAnchor, constant: 29).isActive = true
bannerText.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bannerText.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
bannerText.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
slogan.topAnchor.constraint(equalTo: bannerText.bottomAnchor, constant: 4).isActive = true
slogan.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
slogan.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
slogan.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
How to fix this problem?
Your constraints are wrong.
bannerText.bottomAnchor = view.bottomAnchor
slogan.topAnchor = bannerText.bottomAnchor(view.bottomAnchor)
slogan.bottomAnchor = view.bottomAnchor
This will set the height of the second text view to zero. You should have a height constraint for at least one of the text views rather than pinning both of them to the bottom of the superView.
You have pinned the bannerText to the bottom of the logo (offset by 29) and the left, right and bottom of the view.
You have then pinned the slogan to the bottom of the bannerText (offset by 4) and the left, right and bottom of the view.
That's not going to work because the bottom of the bannerText and the bottom of the view are the same and as the top of the slogan is the bottom of the view the top of slogan ends up being the bottom of the view and therefore it is outside the view (the constraints probably break as well).
You need to fix the constraints based on what you want to see.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.