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

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

问题描述

我想要 UITextView 来填充 superView ,这是一个简单的 UIView UIViewController 实例中。

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

似乎我无法生成 UITextView 仅使用API​​指定的 autoresizingMask autoresizesSubviews 。如此处所示设置这些没有任何作用; 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.

推荐答案

自动调整并不意味着子视图将占用其超级视图的大小。它只是意味着只要superview的边界发生变化,它就会相对于superview的大小变化进行调整。因此,首先,您必须将子视图的大小设置为正确的值。然后,自动调整掩码将处理将来的大小更改。

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天全站免登陆