更改对象的框架大小后更新自动布局

时间:2018-09-27 06:44:05

标签: swift xcode ios-autolayout

我正在使用自动布局创建视图,结果是这样。

enter image description here

视图加载后,然后获取一些文本数据并填充UITextView的数据。 “关于我”项可以是多行,因此我调整了该特定框架的大小。然后,我得到以下信息。

enter image description here

您看到关于我的文本视图如何覆盖下一个字段吗?如何使用与我有关的新的textview大小来调整自动布局的大小?我搜索了一下,发现了一些使用setNeedsLayout和layoutIfNeeded的建议,但是都没有用。

我正在如下设置自动布局:

    inputsContainerView.addSubview(ageInput)
    ageInput.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
    ageInput.topAnchor.constraint(equalTo: inputsContainerView.topAnchor, constant: 10).isActive = true
    ageInput.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
    ageInput.heightAnchor.constraint(equalToConstant: 60).isActive = true

    inputsContainerView.addSubview(genderInput)
    genderInput.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
    genderInput.topAnchor.constraint(equalTo: ageInput.bottomAnchor).isActive = true
    genderInput.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
    genderInput.heightAnchor.constraint(equalToConstant: 60).isActive = true

    inputsContainerView.addSubview(aboutInput)
    aboutInput.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
    aboutInput.topAnchor.constraint(equalTo: genderInput.bottomAnchor).isActive = true
    aboutInput.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
    aboutInput.heightAnchor.constraint(equalToConstant: 60).isActive = true

    inputsContainerView.addSubview(memberSinceInput)
    memberSinceInput.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
    memberSinceInput.topAnchor.constraint(equalTo: aboutInput.bottomAnchor).isActive = true
    memberSinceInput.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
    memberSinceInput.heightAnchor.constraint(equalToConstant: 60).isActive = true

视图加载后,我获取数据并使用以下功能调整textview框架的大小:

func resizeTextView(_ textView: UITextView) {
    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame
}

1 个答案:

答案 0 :(得分:1)

如果我是你,我将使用UITableView创建此表单,并将标签和UITextView添加到单元格中。

您可以像下面这样进行UITableViewCell,将标签的高度设置为60并自动布局。 UITextView还使用自动布局并适合单元格的底部。

import UIKit

class UserDetailCell: UITableViewCell {

    var userDetailLabel : UILabel = {
        var label = UILabel()
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textColor = UIColor.lightGray
        return label
    }()

    var userDetailTextView : UITextView = {
        var tv = UITextView()
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.isScrollEnabled = false
        return tv
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupUI()
    }

    func setupUI(){
        addSubview(userDetailLabel)

        NSLayoutConstraint.activate([
                userDetailLabel.topAnchor.constraint(equalTo: topAnchor),
                userDetailLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 8),
                userDetailLabel.rightAnchor.constraint(equalTo: rightAnchor),
                userDetailLabel.heightAnchor.constraint(equalToConstant: 60)
            ])

        addSubview(userDetailTextView)
        NSLayoutConstraint.activate([
            userDetailTextView.topAnchor.constraint(equalTo: userDetailLabel.bottomAnchor),
            userDetailTextView.leftAnchor.constraint(equalTo: leftAnchor, constant: 8),
            userDetailTextView.rightAnchor.constraint(equalTo: rightAnchor),
            userDetailTextView.bottomAnchor.constraint(equalTo: bottomAnchor)
            ])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

然后您的UIViewController应该如下所示。我在cellForRowAt indexPath方法内将委托设置为UITextView。由于我设置了委托,因此将调用textViewDidChange委托方法。它写在扩展内。

import UIKit

class UserDetailsController: UITableViewController {

    let cellId = "cell"
    var person = Person(myAge: 20, myGender: "Male", aboutMe: "Hello my name is jake waisee. What is your name? goayngeHello my name is jake waisee. What is your name? goayngeHello my name is jake waisee. What is your name? goayngeHello my name is jake waisee. What is your name? goaynge")

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UserDetailCell.self, forCellReuseIdentifier: cellId)
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 100
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 3
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserDetailCell
        cell.userDetailTextView.delegate = self
        cell.userDetailTextView.tag = indexPath.row
        if indexPath.row == 0{
            cell.userDetailLabel.text = "Age"
            cell.userDetailTextView.text = "\(person.age)"
        }else if indexPath.row == 1{
            cell.userDetailLabel.text = "Gender"
            cell.userDetailTextView.text = person.gender
        }else if indexPath.row == 2{
            cell.userDetailLabel.text = "About me"
            cell.userDetailTextView.text = person.aboutMe
        }

        return cell
    }

}


extension UserDetailsController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        print(textView.text)
        if textView.tag == 2 {
            person.aboutMe = textView.text
        }else if textView.tag == 0 {
            person.age = Int(textView.text) ?? 0
        }else if textView.tag == 1 {
            person.gender = textView.text
        }

        //this will keep the textview growing as we type
        tableView.beginUpdates()
        tableView.endUpdates()
    }
}

希望这对您有所帮助。您的用户界面应如下所示。 enter image description here