37

I would like a UITextView to fill its superView, which is a plain UIView inside a UIViewController instance.

It seems that I cannot make the UITextView do this solely by using the API-specified properties of autoresizingMask and autoresizesSubviews. Setting these as shown here does nothing; the UITextView remains small even though its superView fills the screen.

// use existing instantiated view inside view controller;
// ensure autosizing enabled
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|
                             UIViewAutoresizingFlexibleWidth;
// create textview
textView = [[[UITextView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 1, 1)];
// enable textview autoresizing
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];
// add textview to view
[self.view addSubview:textView];

However, if I instantiate my own view inside the view controller, replacing its '.view' property, then everything works as expected, and the textView fills its superview:

// reinstantiate view inside view controller
self.view = [[UIView alloc]init];
// create textview
textView = [[[UITextView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 1, 1)];
// enable textview autoresizing
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];
// add textview to view
[self.view addSubview:textView];

I've tried both code chunks inside all of these initialisers/methods, and the same situation arises in every case:

-(id)init;
-(id)initWithFrame:(CGRect)frame;
-(void)viewDidLoad;

I realise that reinstantiating the '.view' of the UIViewController is fugly, can anyone explain what I'm doing wrong? I presumed I could overcome the issue by having initial frame-setting code in my UIViewController to resize the UITextView once, and thereafter autoresizing would operate as desired.

-(void)viewDidLoad {
    textView.frame = self.view.frame;
}

... but seemingly the view.frame is not set up at this stage, it does not have its '.size' values defined, so once again textView remains small.

What's the proper way of achieving what I want? Must I explicitly specify the fullscreen dimensions via UITextView:initWithFrame to get it to fill its superview?

I'd be grateful for any advice you can offer.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
KomodoDave
  • 7,239
  • 10
  • 60
  • 92
  • 1
    Don't `autorelease` before you `init`. The correct order is ` ... alloc] init] autorelease]`. `UIView` should be fine, but `NSString`s and other class clusters will leak if you use `autorelease` and `init` in the wrong order. Sorry for not being able to help your real question, though. – Yuji Feb 07 '11 at 11:40
  • 1
    Thanks Yuji, I appreciate the tip - something I wasn't aware of! – KomodoDave Feb 08 '11 at 10:50

2 Answers2

90

Autoresizing does not mean that the subview will take up the size of its superview. It just means that it will resize relative to the size change of its superview whenever the superview's bounds change. So initially, you have to set the size of the subview to the correct value. The autoresizing mask will then deal with size changes in the future.

This is all you need:

textView = [[[UITextView alloc] autorelease] initWithFrame:self.view.bounds];
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 1
    I put this into loadView (making sure to call [super loadView] first) and it does the job nicely :) Many thanks, Ole – KomodoDave Feb 08 '11 at 10:49
  • For Swift it looks like this: `var textView = UITextView(frame: self.view.bounds) textView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight` – martinmose Sep 16 '15 at 17:15
  • 1
    For Swift 2 take this: `textView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]` – Peter Kreinz Nov 10 '15 at 22:21
0

And here Swift 3 solution:

myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
pooopy
  • 311
  • 2
  • 9