xcode/iOS:自动调整大小以填充视图 - 显式帧大小是必不可少的吗? [英] xcode/iOS: Autoresize to fill a view - explicit frame size is essential?

查看:14
本文介绍了xcode/iOS:自动调整大小以填充视图 - 显式帧大小是必不可少的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like a UITextView to fill its superView, which is a plain UIView inside a UIViewController instance.

似乎我无法仅通过使用 API 指定的 autoresizingMaskautoresizesSubviews 属性来让 UITextView 做到这一点.如下所示设置这些没有任何作用;UITextView 仍然很小,即使它的 superView 填满了屏幕.

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

但是,如果我在视图控制器中实例化我自己的视图,替换它的 '.view' 属性,那么一切都按预期工作,并且 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;

我意识到重新实例化 UIViewController 的.view"很糟糕,谁能解释一下我做错了什么?我认为我可以通过在我的 UIViewController 中使用初始框架设置代码来调整 UITextView 的大小来解决这个问题,然后 autoresizing 将作为需要.

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

...但是在这个阶段似乎没有设置 view.frame,它没有定义它的 '.size' 值,所以 textView 仍然很小.

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

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

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.

推荐答案

自动调整大小并不意味着子视图将占据其父视图的大小.这只是意味着只要父视图的边界发生变化,它就会相对于其父视图的大小变化调整大小.所以最初,您必须将子视图的大小设置为正确的值.自动调整大小掩码将在未来处理大小变化.

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.

这就是您所需要的:

textView = [[[UITextView alloc] autorelease] initWithFrame:self.view.bounds];
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];

这篇关于xcode/iOS:自动调整大小以填充视图 - 显式帧大小是必不可少的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆