0

I've published an app for the iPad and I'm working on making it universal. I have a UITextView that I want to resize to fit its contents. The TextView is created via storyboard and referenced through an IBOutlet. I read the answer to this question How do I size a UITextView to its content? and am using the code to resize the TextView. This code works perfectly on the iPad, but when I run it on the iPhone, it doesn't resize. I've been working on this problem for many hours, and it has been killing me.

//self.speciesDescription is the TextView in Question here:

//set the text
self.speciesDescription.text=[self.modal getdescriptionForSpecies:self.plantDisplayed];

//call the method below
[self resizeTextView:self.speciesDescription];

//resizes the text view to fit the text
-(void)resizeTextView:(UITextView *)textView {
    CGRect contentFrame=textView.frame;
    contentFrame.size.height=textView.contentSize.height;
    textView.frame=contentFrame;
}

If I call initWithFrame: on the TextView before trying to resize it works fine, but it would save me bunches of time and code if I don't have to do this. Doesn't the StoryBoard call initWithFrame: automatically anyways?

edit: I ran some tests today and realized that the contentSize property of the textView always has a width of 0 and the height is over 16000. Any ideas why that might be?

Community
  • 1
  • 1
67cherries
  • 6,931
  • 7
  • 35
  • 51

1 Answers1

0

The storyboard initialises its views with initWithCoder: constructor, not initWithFrame:. If you do some custom initialisation, make sure you do it inside initWithCoder: method.

Adam
  • 26,549
  • 8
  • 62
  • 79
  • Do you have any idea why It would work on the iPad but not the iPhone though? – 67cherries Mar 23 '13 at 22:02
  • I have not seen your code. I don't know what custom initialisation you do for your view. Maybe you have some device specific code which works correctly on iPad but not on iPhone. – Adam Mar 23 '13 at 22:09