Swift TextView的发展方向

时间:2018-08-09 18:19:50

标签: ios swift autolayout

所以我在uiview中有一个textview。我的问题是,当textview转到下一行以及uiview时,如何使textivew向上生长。

var textheightcontraint : NSLayoutConstraint!
var viewheightconstraint : NSLayoutConstraint!


func setup4(){
    view.addSubview(colorview)
    colorview.topAnchor.constraint(equalTo: customtableview.bottomAnchor).isActive = true
    colorview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    colorview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    viewheightconstraint = colorview.heightAnchor.constraint(equalToConstant: 44)
    viewheightconstraint.isActive = true
    colorview.backgroundColor = UIColor.lightGray

    colorview.addSubview(customtextview2)
    customtextview2.backgroundColor = .white
    customtextview2.leftAnchor.constraint(equalTo: colorview.leftAnchor).isActive = true
    customtextview2.rightAnchor.constraint(equalTo: colorview.rightAnchor, constant: -20).isActive = true
    customtextview2.bottomAnchor.constraint(equalTo: colorview.bottomAnchor).isActive = true
    textheightcontraint = customtextview2.heightAnchor.constraint(equalToConstant: 39)
    textheightcontraint.isActive = true
    customtextview2.delegate = self

}
func setuptextview(){
    let fixedWidth = customtextview2.frame.size.width
    let newSize = customtextview2.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    self.textheightcontraint.constant = newSize.height
    self.viewheightconstraint.constant = newSize.height
    self.view.layoutIfNeeded()
}
func textViewDidChange(_ textView: UITextView) {
    setuptextview()
}

1 个答案:

答案 0 :(得分:0)

如果我理解您的问题,您希望文本视图(及其包含的视图)保持其底部在同一位置,并在键入时向上扩展吗?

为此,请禁用文本视图中的滚动,并设置约束,以便将包含视图的底部限制在y位置:

//
//  ViewController.swift
//
//  Created by Don Mag on 8/9/18.
//

import UIKit

class ViewController: UIViewController {

    var theContainingView: UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    var theTextView: UITextView = {
        let v = UITextView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .yellow

        theContainingView.backgroundColor = .cyan

        theContainingView.addSubview(theTextView)

        view.addSubview(theContainingView)

        NSLayoutConstraint.activate([

            theTextView.topAnchor.constraint(equalTo: theContainingView.topAnchor, constant: 8.0),
            theTextView.bottomAnchor.constraint(equalTo: theContainingView.bottomAnchor, constant: -8.0),
            theTextView.leadingAnchor.constraint(equalTo: theContainingView.leadingAnchor, constant: 8.0),
            theTextView.trailingAnchor.constraint(equalTo: theContainingView.trailingAnchor, constant: -8.0),

            theContainingView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40.0),
            theContainingView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40.0),

            // Constrain the Bottom of the containing view. As the textView grows (or shrinks) with input,
            // the TOP of the view will move up or down
            theContainingView.bottomAnchor.constraint(equalTo: view.topAnchor, constant: 300.0),

            ])

        theTextView.isScrollEnabled = false
        theTextView.text = "This is the starting text."

    }

}

结果:

enter image description here