我有一个带有许多子视图控制器的 UINavigationController,其中一些是容器控制器。最后添加的子 UIViewController(绿色背景)的 UIView 框架布局有它的父背景(黄色),当它不应该时。
如何布局所有内容,以便所有视图都适合导航栏下方的屏幕范围,而不会出现奇怪的偏移?
UIViewController 层次结构,从我的 AppDelegate 开始:
UINavigationController rootViewController:FrontFacadeViewController < UIViewController
view (0,0) dim:(320x460)
=> StandardCoverViewController < UIViewController
added as addSubview of FrontFacadeViewController's view]
view: (0,0) dim:(320x460)
=> ConfigurationViewController < UIViewController
added by pushing onto self.navigationController
view: (0,20) (320x460)
=> GearViewController < UIViewController
added as subview of ConfigurationViewController's view
view: (0,0) (320x416)
以下是所有视图控制器的创建方式:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FrontFacadeViewController *frontController = [[FrontFacadeViewController alloc]
initWithBundle:[NSBundle mainBundle]];
_appNavigationController = [[UINavigationController alloc]
initWithRootViewController:frontController];
}
FrontFacadeViewController:
// FrontFacadeViewController determines a particular view controller to present
// based on some business logic.
// Because of this, it adds child UIViewControllers using container controllers.
//
- (void)viewDidLoad {
StandardCoverViewController *controller = [StandardCoverViewController alloc] initWithNibName:nil bundle:self.nibBundle];
[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
}
- (IBAction)onButtonPress {
ConfigurationViewController *controller = [[ConfigurationViewController alloc] initWithNibName:nil bundle:self.nibBundle];
[self.navigationController pushViewController:controller animated:YES];
}
ConfigurationViewController:(黄色背景)
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor yellowColor]];
GearViewController *controller = [[GearViewController alloc] initWithNibName:nil bundle:self.nibBundle];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
}
GearViewController:(绿色背景)
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor greenColor]];
// nothing special
}
我做这一切都错了吗?