快速的第二个textview不可见

时间:2018-01-13 23:12:22

标签: ios swift constraints

我有一个iOS项目,我正在使用Xcode 9.2和Swift4。我有一个UITextView,但第二个UITextView不可见。

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();

和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
}

如何解决这个问题?

Screen

2 个答案:

答案 0 :(得分:2)

你的约束是错误的。

bannerText.bottomAnchor = view.bottomAnchor

slogan.topAnchor = bannerText.bottomAnchor(view.bottomAnchor)

slogan.bottomAnchor = view.bottomAnchor

这会将第二个文本视图的高度设置为零。 您应该至少对其中一个文本视图有一个高度约束,而不是将它们都固定在superView的底部。

答案 1 :(得分:0)

您已将bannerText固定到徽标的底部(偏移29)以及视图的左侧,右侧和底部。

然后您将标语固定在bannerText的底部(偏移4)以及视图的左侧,右侧和底部。

这不起作用,因为bannerText的底部和视图的底部是相同的,并且标语的顶部是视图的底部,标语的顶部最终是视图的底部,因此它在视图之外(约束可能也会破坏)。

您需要根据要查看的内容修复约束。