ios - Setting dynamic height on UItextview to fit content programmatically -


just programmatically coding, i'm trying achieve below image. textview , containerview embedded in scrollview although not shown in image.

enter image description here

here code this.

override func viewdidload() {     super.viewdidload()     let margin = cgfloat(10)     textview =  uitextview()     textview.text = "asdadaasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadaadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadaddadd" let contentsize = textview.sizethatfits(textview.bounds.size) textview.frame = cgrectmake(margin, 0, self.view.frame.width - 2 * margin, contentsize.height)   let scrollview = uiscrollview(frame: cgrect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 50)) let containerview = uiview(frame: cgrectmake(margin, contentsize.height + margin, self.view.frame.width - 2 * margin, self.view.frame.height))  let button1 = uibutton(frame:  cgrectmake(margin, contentsize.height + containerview.frame.height + 2 * margin, (view.frame.width - 2 * margin)/2, 30))  scrollview.contentsize = cgsize(width: self.view.frame.width, height: button1.frame.origin.y + button1.frame.height + margin)  self.view.addsubview(scrollview) scrollview.addsubview(textview) scrollview.addsubview(containerview)  scrollview.backgroundcolor = uicolor.bluecolor() containerview.backgroundcolor = uicolor.yellowcolor() textview.backgroundcolor = uicolor.greencolor() } 

the problem textview's height not dynamic show content in textview.

i've confirmed if add below code in viewdidappear, make height of textview dynamic stacked under container view because below code runs after viewdidload configured everything.

let contentsize = textview.sizethatfits(textview.bounds.size) textview.frame = cgrectmake(margin, 0, self.view.frame.width - 2 * margin, contentsize.height) 

what best approach kind of problem? think caused due rendering order in viewdidload i'm not 100% sure. great if can talk rendering order too.

to answer original question, call sizethatfits has no idea how tall textview should because has no idea how wide want be. think before sizethatfits line want following:

textview.frame.size.width = self.view.frame.width - 2 * margin 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -