viewDidLoad中的边界和框架大小 [英] Bounds and Frame size in viewDidLoad

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

问题描述

我推送一个视图控制器( mainVC )并向其中添加一些子视图。这些视图大多数都是按需构建的,因为用户会采取一些措施。



构建每个视图时,我指的是 mainVC.view。 .bounds 来调整视图的大小。基于需求大小构建的那些很好,但是第一个(我添加到 viewDidLoad 中)似乎并没有考虑隐藏的导航栏或未隐藏的工具栏-我不确定是哪个。



其他问题似乎向我保证,viewDidLoad是引用边界和添加这些子视图的正确位置。我误会了吗?记录结果如下:



viewDidLoad 中,边界高度为548。



viewWillAppear 中,边界高度为460(-44 x 2)。在 viewWillAppear 中,我隐藏了导航栏,但是前后的高度相同。



以后的所有时间,则界限高度为504(-44 x 1)。



为什么在我尝试在中读取界限高度时,界限高度没有更新viewDidLoad

解决方案

视图控制器生命周期在适用于iOS的View Controller编程指南



简而言之,当第一次调用 -view 时,将调用 -viewDidLoad 。即使视图控制器对视图所做的第一件事就是设置框架,它仍然需要访问该视图。

  UIViewController * controller = [MyViewController myViewController]; 

// -viewDidLoad尚未被调用(如果+ myViewController不调用-view。)

UIView * view = [controller view];

// -viewDidLoad已被调用。

view.frame = [UIApplication sharedApplication] .delegate.window.bounds;

即使您使用 controller.view.frame 发生相同的逻辑顺序。 -viewDidLoad 将始终被带有未反序列化的视图的调用,而该视图尚未嵌入视图层次结构中。






更新



这种情况以前还不清楚,我指出了为什么 -viewDidLoad 中的视图设置错误。只有在 -viewDidLoad 之后,视图控制器的视图才会添加到视图层次结构中。



如果必须将框架设置为正确的尺寸,则可以使用IB设置您知道的尺寸。需要或(如果您不使用IB)在 -loadView 中设置框架。这种方法是有缺陷的,因为它静态地将帧的大小设置为一个值,该值在调用 -viewDidLoad 之后会被更改(诸如调用状态栏之类的东西)。 / p>




更新2



您需要将添加的子视图与子视图的位置和大小分开。



如果您是从Storyboard或Nib中加载的,那么您是正确的,应该在其中添加其他视图 -viewDidLoad 。如果以编程方式加载,则所有子视图都将添加到 -loadView 中。



第二个任务是定位和定位调整子视图的大小。首选方法是使用 UIView.autoresizingMask ,但要获得更精确的布局,请 KVO view.bounds并在视图控制器的视图更改大小时调整自定义子视图。


I push a view controller (mainVC) and add some subviews to it. Most of these views are built on-demand, as the user takes some action.

When I build each view, I refer to mainVC.view.bounds to size the view. The ones that are built on demand size just fine, but the first one (which I add in viewDidLoad) does not seem to account for either the hidden navigation bar or the unhidden toolbar - I'm not sure which.

Other questions seem to assure me that viewDidLoad is the correct place to refer to the bounds and to add these subviews. Am I misinformed? Logging yields the following:

In viewDidLoad the bounds height is 548.

In viewWillAppear the bounds height is 460 (-44 x 2). In viewWillAppear I hide the navigation bar, but the height is the same before and after.

At all later times, the bounds height is 504 (-44 x 1).

Why is the bounds height not updated by the time I attempt to read it in viewDidLoad?

解决方案

The view controller lifecycle is described in View Controller Programming Guide for iOS.

In short, when -view is called the first time, -viewDidLoad is called. Even if the very first thing a view controller does to a view is set the frame, it still needs to access the view.

UIViewController *controller = [MyViewController myViewController];

// -viewDidLoad has not been called (if +myViewController doesn't call -view.)

UIView *view = [controller view];

// -viewDidLoad has been called.

view.frame = [UIApplication sharedApplication].delegate.window.bounds;

Even if you use controller.view.frame the same logical sequence happens. -viewDidLoad will always be a called with a freshly deserialized view that has not been embedded into the view hierarchy.


Update

It case it wasn't clear earlier, I was pointing out why the bounds of view are not set correctly in -viewDidLoad. It is only after -viewDidLoad does the view controller's view get added to the view hierarchy. This is when the final frame of the view is set.

If you must have the frame be the right dimensions, you can use IB to set the dimensions that you know you'll need or (if you don't use IB) set the frame in -loadView. This approach is flawed because it statically sets the size of the frame to a value that may be changed after -viewDidLoad is called (things like the in-call status bar).


Update 2

You need to split the adding subviews from positioning and sizing subviews.

If you are loading from a Storyboard or a Nib, then you are correct, additional views needed should be added in -viewDidLoad. If you're loading programmatically, then all subviews are added in -loadView.

The second task is positioning and sizing subviews. The preferred method is to use UIView.autoresizingMask, but for more precise layout, KVO view.bounds and adjust your custom subviews when view controller's view changes its size.

这篇关于viewDidLoad中的边界和框架大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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