xcode/iOS:自动调整大小以填充视图 - 显式的帧大小是必不可少的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4920798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:49:12  来源:igfitidea点击:

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

xcodeiosuiviewautoresizeautoresizingmask

提问by KomodoDave

I would like a UITextViewto fill its superView, which is a plain UIViewinside a UIViewControllerinstance.

我想要一个UITextView来填充它的superView,它是UIView一个UIViewController实例中的一个普通的。

It seems that I cannot make the UITextViewdo this solely by using the API-specified properties of autoresizingMaskand autoresizesSubviews. Setting these as shown here does nothing; the UITextViewremains small even though its superViewfills the screen.

似乎我无法UITextView仅通过使用 API 指定的autoresizingMask和属性来做到这一点autoresizesSubviews。如下所示设置这些没有任何作用;在UITextView依然很小,即使它superView充满屏幕。

// 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:

但是,如果我在视图控制器中实例化我自己的视图,替换它的 '.view' 属性,那么一切都按预期工作,并且 textView 填充了它的超级视图:

// 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 UIViewControlleris fugly, can anyone explain what I'm doing wrong? I presumed I could overcome the issue by having initial frame-setting code in my UIViewControllerto resize the UITextViewonce, and thereafter autoresizingwould operate as desired.

我意识到重新实例化 '.view'UIViewController很糟糕,谁能解释我做错了什么?我认为我可以通过在我的初始框架设置代码中UIViewController调整UITextView一次大小来克服这个问题,然后autoresizing可以根据需要进行操作。

-(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.

...但看起来 view.frame 在这个阶段没有设置,它没有定义它的 '.size' 值,所以 textView 再次保持小。

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

实现我想要的正确方法是什么?我必须通过明确指定全屏尺寸UITextView:initWithFrame才能让它填充其超级视图吗?

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

如果您能提供任何建议,我将不胜感激。

回答by Ole Begemann

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

回答by Philippe H. Regenass

And here Swift 3solution:

这里是Swift 3解决方案:

myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]