正确的loadView实施 [英] Correct loadView implementation

查看:79
本文介绍了正确的loadView实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple的文档未说明loadView的正确实现是什么.

Apple's docs do not say what the correct implementation is for loadView.

我发现,如果您这样实现loadView:

I've found that if you implement loadView like this:

- (void)loadView
{
    self.view = [[UIView alloc] init];
}

...然后,您得到的行为与根本不执行的行为不同.特别是,在一个20行的项目中,我发现viewWillAppear的self.view框架大小为零.除非您使用Apple的默认版本的loadView.

...then you get different behaviour than if you don't implement it at all. In particular, in one 20-line project, I've found that viewWillAppear is called with a zero-size frame for self.view - unless you use Apple's default version of loadView.

在Google上,有很多教程"提供了明显错误的loadView实现-例如强制将大小设置为(320,480),因为教程作者发现如果这样做,它就可以工作".

Looking on Google, there are lots of "tutorials" that provide obviously-wrong loadView implementations - e.g. force-setting the size to (320,480) because the tutorial author "found that it works if I do this".

我想知道正确的实现方式.

I'd like to know what the correct implementation should be.

注意:在上面的示例中,我将其添加到AppDelegate内部的视图层次结构中,如下所示:

NB: in my example above, I'm adding it to the view hierarchy inside AppDelegate like this:

[self.window addSubview:(UIViewController*).view];

我相信,在存在UINavigationController或UITabBarController的情况下,Apple会做一些额外的魔术,作为副作用,它会使单行loadView实现正常工作.但是我想正确地编写它,以便它始终正常工作!

I believe that in the presence of a UINavigationController or UITabBarController, Apple does some extra magic that - as a side-effect - causes a one-line loadView implementation to work OK. But I want to write it correctly, so that it always works!

注意:我已经尝试在根视图上设置自动调整大小蒙版,但是它不会改变发生的情况:

NB: I've tried setting the autoresizing mask on the root view, but it doesn't change what happens:

- (void)loadView
{
    self.view = [[UIView alloc] init];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

推荐答案

-loadView 的默认实现创建视图或加载NIB .据我所知,在-loadView中无法知道视图创建时的最终大小.因此,默认视图大小设置为UIScreen.mainScreen.bounds.这是因为在-viewDidLoad和其他方法中使用零帧视图可能很困难.

Default implementation of -loadView creates the view or loads NIB. As far as I know, there is no way to know the final size of the view at time of creation in -loadView. So the default view size is set to UIScreen.mainScreen.bounds. This is because it may be difficult to work with zero frame view in -viewDidLoad and other methods.

您的单行实现可能如下所示:

Your one-line implementation may look like this:

- (void)loadView {
    self.view = [[UIView alloc] initWithFrame:UIScreen.mainScreen.bounds];
}

您不需要设置自动调整大小蒙版,因为您不知道在什么上下文中显示视图.呼叫者负责为您设置正确的帧,自动调整蒙版的大小和类似的属性.

You don't need to set the autoresizing mask, because you don't know in what context the view will be displayed. The caller is responsible to set you correct frame, autoresizing mask and similar properties.

UINavigationController方法想象一下:

// we are pushing new VC, view is accessed for the first time
pushedVC.view.frame = CGRectMake(...);

它设置了正确的帧,但是您的-loadView-setFrame:之前被称为之前.因此,在-viewDidLoad中,您只有一个临时的非零帧,以便能够设置子视图和内部自动调整大小.此后,将为您设置正确的帧,然后在-viewWillAppear:中获得最终的帧.

It is setting the correct frame, but your -loadView is called just before that -setFrame:. So during -viewDidLoad you have temporary non-zero frame, just to be able to setup subviews and internal autoresizing. After this, the correct frame is set to you and in -viewWillAppear: you have final frame.

这篇关于正确的loadView实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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