UIViewController层次结构中带有容器控制器和UINavigationController的意外UIView偏移量

时间:2012-08-14 18:17:41

标签: ios5 uiview uiviewcontroller

我有一个带有许多子视图控制器的UINavigationController,其中一些是容器控制器。最后添加的子UIViewController(绿色背景)的UIView框架布局具有其父背景(黄色)到不应该的时候。

enter image description here

如何布置所有内容以使所有视图都适合导航栏下方屏幕的边界而没有奇怪的偏移?

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
}

我这样做是错的吗?

0 个答案:

没有答案