ios - Change ScrollView height based on content of the View -
i parse json data , show using various uitextview, used scrollview created via storyboard has height of 1000px can't show whole data since it's long fit scrollview, how can update programmatically in order have height based on content view has show?
go storyboard , remove height constraint of uitextviews. in order avoid warnings in storyboard, select intrinsic size -> place holder each uitextview in scrollview. option located in size inspector (right pane). tell xcode know don't want specify height, rather want them based on intrinsiccontentsize.
finally, create new class 'fitcontenttextview' inherits uitextview. in storyboard make uitextview's want have enough size fit content of type fitcontenttextview
- (void)settext:(nsstring *)text { [self setscrollenabled:yes]; [super settext:text]; [self invalidateintrinsiccontentsize]; } - (cgsize)intrinsiccontentsize { cgsize size = [self contentsize]; return size; }
the setscrollenabled thing in settext important. method contentsize returns enough size fit text if srolling enabled. otherwise, return the textview's bounds.
also, calling invalidateintrinsiccontentsize every time set text makes sure autolayout call method intrinsiccontentsize before drawing textview, , add methods it.
lastly, remember pin scrollview's subviews vertically superview top bottom. way autolayout can update contentsize of srollview fit subviews.
Comments
Post a Comment