Hos Ap
Hos Ap

Reputation: 1198

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.

enter image description here

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?

Upvotes: 2

Views: 535

Answers (1)

vbrittes
vbrittes

Reputation: 200

It's possible to do it using autolayout + UITableViewAutomaticDimension, check this out: http://candycode.io/self-sizing-uitextview-in-a-uitableview-using-auto-layout-like-reminders-app/

Upvotes: 1

Related Questions