导致textview隐藏的约束

时间:2015-11-02 20:33:06

标签: ios swift uitextview

如果我使用文本字段,则会出现该对象,但如果我使用textview,则该对象不会出现。我相信问题在于我的约束?

    commentTextView.text = "test text"
    commentTextView.translatesAutoresizingMaskIntoConstraints = false
    commentTextView.backgroundColor = UIColor.redColor()
    commentTextView.font = UIFont.boldSystemFontOfSize(bodyFontSize)
    commentTextView.textColor = UIColor.whiteColor()

    containerView.addSubview(commentTextView)

    let views: [String : AnyObject] = [
        "containerView": containerView,
        "titleLabel": titleLabel,
        "commentTextView": commentTextView,
        "closeButton": closeButton]

    view.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|[containerView]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views))

    view.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|[containerView]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views))

    containerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|[titleLabel]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views))

    containerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|[commentTextView]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views))

    containerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|[closeButton]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views))

    containerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|-(>=30)-[titleLabel]-" +
            "[commentTextView]-[closeButton]-(>=10)-|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views))

任何有关让我的textview出现的帮助都将非常感谢

1 个答案:

答案 0 :(得分:0)

尝试在textview上设置高度约束:

commentTextView.addConstraint(NSLayoutConstraint(item: commentTextView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0))

文本字段知道其高度而不显式设置(至少使用默认边框样式)。 textview没有。