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

梦想的初衷 提交于 2019-11-27 09:50:51

问题


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.


回答1:


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



回答2:


And here Swift 3 solution:

myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]


来源:https://stackoverflow.com/questions/4920798/xcode-ios-autoresize-to-fill-a-view-explicit-frame-size-is-essential

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!