查看viewWillAppear:和viewDidAppear之间的框架更改: [英] View frame changes between viewWillAppear: and viewDidAppear:

查看:117
本文介绍了查看viewWillAppear:和viewDidAppear之间的框架更改:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中发现了一个奇怪的行为,其中连接的 IBOutlet 在我的视图控制器中的调用到 viewWillAppear之间的连接视图框架: viewDidAppear:。这是我的 UIViewController 子类中的相关代码:

I have discovered a strange behavior in my application, where a connected IBOutlet has its connected view's frame between the calls in my view controller to viewWillAppear: and viewDidAppear:. Here is the relevant code in my UIViewController subclass:

-(void)viewWillAppear:(BOOL)animated {
    NSLog(@"%@", self.scrollView);
}

-(void)viewDidAppear:(BOOL)animated {
    NSLog(@"%@", self.scrollView);
}

以及产生的日志输出:

MyApp[61880:c07] <UIScrollView: 0x1057eff0; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x10580100>; layer = <CALayer: 0x1057f210>; contentOffset: {0, 0}>
MyApp[61880:c07] <UIScrollView: 0x1057eff0; frame = (0 44; 320 416); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x10580100>; layer = <CALayer: 0x1057f210>; contentOffset: {0, 0}>

这清楚地表明帧在两次调用之间发生变化。我想在 viewDidLoad 方法中对视图进行设置,但是如果内容在我屏幕上无法更改,那么这似乎没用。可能会发生什么?

Which clearly shows that the frame is changing between the two calls. I wanted to do setup with the view in the viewDidLoad method, but if the content is not available for me to change until it is on the screen, that seems pretty useless. What could be happening?

推荐答案

Autolayout 如何做出巨大改变我们设计和开发我们的视图的GUI。其中一个主要区别是 autolayout 不会立即改变我们的视图大小,但只有在触发时,这意味着在特定时间,但我们可以强制它重新计算我们的约束立即或将它们标记为需要布局。它的工作方式类似于 -setNeedDisplay

对我来说,最大的挑战是理解并接受这一点,我们不再需要使用自动调整大小的面具了,框架在放置视图时已成为无用的属性。我们不再需要考虑视图位置,但我们应该考虑如何在彼此相关的空间中看到它们。

当我们想要混合旧的自动调整掩码和自动布局时,就会出现问题。我们应该尽快考虑自动布局实现,并尽量避免在基于自动布局的视图层次结构中混合使用旧方法。
可以使用仅使用自动调整大小的掩码的容器视图,例如视图控制器的主视图,但如果我们不尝试混合,则更好。

我从未使用过故事板,但最合适的是它是正确的。使用Autolayout时,会在autolayout引擎开始计算时设置视图的框架。尝试在超级 - (void)viewDidLayoutSubviews 视图控制器的方法后问同样的事情。
当autolayout引擎完成计算你的方法时调用此方法查看框架。

Autolayout made a huge change in how we design and develop the GUI of our views. One of the main differences is that autolayout doesn't change our view sizes immediately, but only when is triggered, that means at a specific time, but we can force it to recalculate our constraints immediately or mark them as "in need" of layout. It works like -setNeedDisplay.
The big challenge for me was to understand and accept that, we do not need to use autoresizing masks anymore, and frame has become a useless property in placing our views. We do not need to think about view position anymore, but we should think how we want to see them in a space related to each other.
When we want to mix old autoresizing mask and autolayout is when problems arise. We should think about autolayout implementation really soon and try to avoid to mix the old approach in a view hierarchy based on autolayout.
Is fine to have a container view that uses only autoresizing masks, such as a main view of a view controller, but is better if we do not try to mix.
I never used storyboard, but most proably it is correct. Using Autolayout, frame of your views are set when the autolayout engine starts its calculation. Try to ask the same thing right after super of - (void)viewDidLayoutSubviews method of your view controller.
This method is called when the autolayout engine has finished to calculate your views' frames.

这篇关于查看viewWillAppear:和viewDidAppear之间的框架更改:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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