[英]Difference between directly setting a controller's view and adding view as a subview
[英]Adding a view controller's view as a subview changes it's frame?
SomeViewController *someVC = [[SomeViewController alloc] initWithNibName:@"SomeViewController" bundle:nil];
self.someViewController = someVC;
[someVC release];
NSLog(@"height before added as subview: %f", self.someViewController.view.frame.size.height);
[self.containerView addSubview:self.someViewController.view];
NSLog(@"height AFTER added as subview: %f", self.someViewController.view.frame.size.height);
// height before added as subview: 480.000000
// height AFTER added as subview: 540.000000
有什么可能的解釋呢? someViewController的視圖沒有設置任何自動調整大小屬性,containerView不會自動調整其子視圖的大小。 只是將其添加為子視圖,是什么導致框架發生變化?
另一個神秘之處在於,它僅在someViewController的視圖高度> = 480時發生。
這可能是由於視圖的autoresizingMask引起的,其目的是調整大小以適應其超級視圖中的更改。
SomeViewController
是SomeViewController
視圖控制器? 我認為可能發生的一件事是SomeViewController
可能包含一些額外的內容,例如UINavigationController
的頂部欄,並且有時會自動增加大小。 但是,您正在執行的操作是通過將一個視圖控制器的視圖添加到另一視圖控制器來違反HIG。 UIViewController
設計為屏幕的完整視圖,或者屬於專用的視圖控制器,例如UITabBarController
, UINavigationController
或UISplitViewController
。 結果, someViewController
將無法按預期運行,並且不會收到諸如viewDidLoad
調用。 因此,如果您確定未設置自動調整大小屬性,那么我所建議的只是使用UIView
而不是UIViewController
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.