ios - UITextVIew 在屏幕上不可见

标签 ios swift uitextview nslayoutconstraint

我有以下两种观点:

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

我将它们添加到我的 View Controller 中,并具有以下约束:
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 在屏幕上根本不可见。如果我将它的类型更改为 textField 或 UILabel 它工作得很好,所以我不知道我在这里缺少哪些约束。我错过了什么?

最佳答案

您需要向 textView 添加底部 anchor ,因为它继承自 UIScrollView。

正如 Apple Docs 中所写:
https://developer.apple.com/documentation/uikit/uitextview

关于ios - UITextVIew 在屏幕上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37632829/

相关文章:

objective-c - 带有字体名称和粗体的 UITextView 格式

ios - 计算属性中的自调用

ios - 以编程方式从其父 View 中删除 subview 并将其附加到其他父 View

iphone - UITextView 委托(delegate)方法未触发

ios - UITextview 没有完全填满 iPhone 中的文本

ios - 是否可以在 iOS UI 测试中模拟两指滑动?

ios - 阻止在 TableView 的特定单元格中重新排序

ios - 如何使用扩展来简化 Swift 代码?

ios - 向多个收件人发送消息时,快速/核心数据提取到数组

swift - 如何更改不同文件中变量的值?