UIViewController viewDidLoad不正确的宽度/高度 [英] UIViewController viewDidLoad incorrect width/height

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

问题描述

每个人都知道你不能相信UIViewController init / viewDidLoad方法的帧大小;这个:

Everybody knows that you can't trust the frame size on a UIViewController init/viewDidLoad method; this:

- (void)viewDidLoad: {
     NSLog(@"%d", self.view.frame.size.width);
}

会在很多场合打印错误的尺码(特别是它在风景中几乎被打破了)模式)

will print wrong sizes in many occasions (in particular it's pretty much broken in landscape mode)

这实际上会返回始终更正的结果,因此布局子视图是好的:

This will actually return always corrected results so it's good to layout the subviews:

- (void)viewWillAppear: {
     NSLog(@"%d", self.view.frame.size.width);
}

问题是每次出现视图时都会调用viewWillAppears,所以它不是适合分配或添加子视图。所以你最终在接口中声明了每个单独的视图并且你最终得到了我根本不喜欢的巨大的头文件,因为大多数项目在初始设置后不再需要任何操作。

The problem is that viewWillAppears gets called every time the view appears, so it's not suitable to alloc or add subviews. So you end up declaring every single view in the interface and you end up with huge header files that I don't like at all since most of the items don't need any more manipulations after the initial setup.

所以第一个问题是:有没有更好的方法来处理子视图定位?

问题二是非常相关的,假设我有一个UIView的子类,包括其他各个子视图。我在我的界面中声明它,并在我的init / viewDidLoad方法中分配/初始化它。

Question two is very related, let's say I have a subclass of UIView including various others subviews. I declare it inside my interface, and i alloc/init it in my init/viewDidLoad method.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    ...
    menu = [[SNKSlidingMenu alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    ...
}

我们已经知道我们现在需要重新定位它在viewWillAppear中获得更准确的读数

As we already know we now need to reposition it in viewWillAppear to get a more accurate reading

- (void)viewWillAppear:(BOOL)animated{
    ....
    menu.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    ....
 }

问题是当然所有的子视图也需要重新定位。这是由自动调用的layoutSubviews函数完成的,但是我们遇到了同样的问题:所有子视图都需要在SNKSlidingMenu类的接口内声明。有没有办法解决这个问题?

The problem is that of course all the subviews needs to be repositioned as well. This is done by the layoutSubviews function that get called automatically, but we got the same problem: All the subviews need to be declared inside the interface of the SNKSlidingMenu class.. Is there a way around this?

谢谢。

推荐答案

如果你的目标是iOS 5.0或更高版本的你可以使用 viewWillLayoutSubviews viewDidLayoutSubviews 进行更改。

If you are targetting iOS 5.0 or better you can use viewWillLayoutSubviews and viewDidLayoutSubviews to make changes.

至于你的第二个问题,如果你需要以 init 之外的其他方法访问实例变量,你需要保留它,我没有看到它的问题。

As for your second question, if you need access to an instance variable in other method than init, you need to keep it around, I don't see a problem with it.

但是,您可以尝试使用自动布局并在子视图之间设置规则,以便自动为您布局,而无需保留参考。

You can, however, try to use Auto Layouts and set up rules between the subviews so it's automatically laid out for you without the need to keep a reference.

这篇关于UIViewController viewDidLoad不正确的宽度/高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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