xcode/iOS: Autoresize to fill a view - explicit frame size is essential?
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];
KomodoDave
Updated on April 19, 2020Comments
-
KomodoDave about 4 years
I would like a
UITextView
to fill itssuperView
, which is a plainUIView
inside aUIViewController
instance.It seems that I cannot make the
UITextView
do this solely by using the API-specified properties ofautoresizingMask
andautoresizesSubviews
. Setting these as shown here does nothing; theUITextView
remains small even though itssuperView
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 myUIViewController
to resize theUITextView
once, and thereafterautoresizing
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 about 13 yearsI put this into loadView (making sure to call [super loadView] first) and it does the job nicely :) Many thanks, Ole
-
martinmose over 8 yearsFor Swift it looks like this:
var textView = UITextView(frame: self.view.bounds) textView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
-
Peter Kreinz over 8 yearsFor Swift 2 take this:
textView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]