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

78,315

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];
Share:
78,315
KomodoDave
Author by

KomodoDave

Updated on April 19, 2020

Comments

  • KomodoDave
    KomodoDave about 4 years

    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.

  • KomodoDave
    KomodoDave about 13 years
    I put this into loadView (making sure to call [super loadView] first) and it does the job nicely :) Many thanks, Ole
  • martinmose
    martinmose over 8 years
    For Swift it looks like this: var textView = UITextView(frame: self.view.bounds) textView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
  • Peter Kreinz
    Peter Kreinz over 8 years
    For Swift 2 take this: textView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]