简体   繁体   中英

change TextView height - iOS

i have a xib file as tableview header cell which i want it to be self size. inside xib, i have a text view that i want to change its height on a button press. when btn is pressed, the textview's height should be the size to fit the text inside and when btn pressed again, it should resize to just two lines of text.

在此处输入图片说明

i used these lines of code in my btn action

    let contentSize = self.content.sizeThatFits(self.content.bounds.size)
    var frame = self.content.frame
    frame.size.height = contentSize.height
    self.content.frame = frame

it expands the textview but textview overlaps to elements below it. using two lines of code below for self sizing header makes every thing worse and it shows only two lines of textview and cm, like and others disappear.

    tableView.estimatedSectionHeaderHeight = 115.0
    tableView.sectionHeaderHeight = UITableViewAutomaticDimension

how can i solve this?

可以使用 autolayout + UITableViewAutomaticDimension 来做到这一点,请查看: http ://candycode.io/self-sizing-uitextview-in-a-uitableview-using-auto-layout-like-reminders-app/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM