xcode/iOS: Autoresize to fill a view - explicit frame size is essential?

KomodoDave picture KomodoDave · Feb 7, 2011 · Viewed 77.9k times · Source

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.

Answer

Ole Begemann picture Ole Begemann · Feb 7, 2011

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];