UITextVIew在屏幕上不可见

4

我有以下两个视图:

 let captionTextView: UITextView = {
    let textField = UITextView(frame: CGRectMake(0,0, 250, 100))
    textField.text = "Enter caption..."
    textField.textColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0)
    textField.editable = true
    textField.backgroundColor = UIColor.blueColor()
    textField.layer.cornerRadius = 5
    textField.layer.borderColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0).CGColor
    textField.layer.borderWidth = 1
    textField.font = UIFont.boldSystemFontOfSize(15)
    return textField
}()

let infoLabel : UILabel = {
    let myLabel = UILabel()
    myLabel.textColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0)
    myLabel.text = "Testing"
    myLabel.font = UIFont.systemFontOfSize(16.0)
    myLabel.textAlignment = NSTextAlignment.Center
    myLabel.adjustsFontSizeToFitWidth = true
    return myLabel
}()

我将以下约束条件添加到我的视图控制器中:
func setupViews() {
    self.view.addSubview(captionTextView)
    self.view.addSubview(infoLabel)
    infoLabel.translatesAutoresizingMaskIntoConstraints = false
    captionTextView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 35))
    self.view.addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: -10))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Top, relatedBy: .Equal, toItem: infoLabel, attribute: .Top, multiplier: 1, constant: 35))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: -10))


}

你需要为你的textView设置高度约束。 - iSashok
这不是由框架指定的吗? - Alk
6
不会像标签一样工作,设置底部或高度。 - Wilson XJ
@mankee 如果删除约束条件会发生什么?那么它会在视图中显示吗? - thephatp
2
我按照@WilsonXJ的建议将底部进行设置,问题已解决。 - Alk
1个回答

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接