首页 文章

当返回键被点击时,如何在原型单元格中更改UITextview的高度?

提问于
浏览
1

我正在开发一个iOS应用程序,我正在使用原型单元格,在这个单元格中我使用的是UITextView .

我正在使用故事板自动布局并设置约束,以便单元格的高度应自动更改 .

I want the UITextView to resize the height so the text I'm typing fits inside it rather than you having to scroll to see the text that overflows. And tableview cell size should also be changed.

这是我的代码:

@IBOutlet weak var tableviewCart: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableviewCart.estimatedRowHeight = 142.0
        self.tableviewCart.rowHeight = UITableViewAutomaticDimension

        // Do any additional setup after loading the view.
    }

enter image description here

class CartMenuItemCell:UITableViewCell{


    @IBOutlet weak var heightConstraintsFoodInstruction: NSLayoutConstraint!
    @IBOutlet var dishImageHeightConstraints: NSLayoutConstraint!
    @IBOutlet weak var dishImageWidthConstraints: NSLayoutConstraint!
    @IBOutlet weak var imgDishImageview: UIImageView!

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!

    @IBOutlet weak var bottomLabelHeightConstraints: NSLayoutConstraint!
    @IBOutlet weak var addMenuItemView: UIView!
    @IBOutlet weak var addMenuItemButton: UIButton!
    @IBOutlet weak var menuItemCountLabel: UILabel!
    @IBOutlet weak var removeMenuItemButton: UIButton!    
}

enter image description here

1 回答

  • 0

    您的tableView配置看起来不错 .

    @IBOutlet weak var tableviewCart: UITableView! {
        didSet {
            tableviewCart.estimatedRowHeight = 142.0
            tableviewCart.rowHeight = UITableViewAutomaticDimension
        }
    }
    

    您只需要为名为 textViewHeightConstraint 的textView添加高度约束,并将其设置为textView 'textView.contentSize.height'

    textViewHeightConstraint.constant = textView.contentSize.height
    

    并布局_1148286_ textView?.superview?.layoutIfNeeded()

相关问题