2012-09-25 42 views
1

我想推ViewController(下一步,调用ViewCon)到NavigationController。 (下一步,请致电NavCon)为什么UIToolBar在NavigationController中消失了?

但是,当ViewCon被推送到NavCon时,ViewCon的工具栏消失了。

当ViewCon未被推入NavCon时,ViewCon的工具栏很适用。

谁知道原因?

enter image description here enter image description here

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     ViewController *viewController1 = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease]; 
     self.viewController = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease]; 
    } else { 
     ViewController *viewController1 = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease]; 
     self.viewController = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease]; 
    } 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

ViewController.m

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIToolbar *toolbar = [[UIToolbar alloc] init]; 
    [toolbar setBarStyle:UIBarStyleBlackTranslucent]; 
    [toolbar sizeToFit]; 

    CGFloat toolbarHeight = [toolbar frame].size.height; 
    CGRect viewBounds = self.view.bounds; 
    CGFloat viewHeight = CGRectGetHeight(viewBounds); 
    CGFloat viewWidth = CGRectGetWidth(viewBounds); 
    CGRect rectArea = CGRectMake(0, viewHeight - toolbarHeight, viewWidth, toolbarHeight); 

    [toolbar setFrame:rectArea]; 

    NSMutableArray *buttons = [[[NSMutableArray alloc] init] autorelease]; 

    UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"preView" style:UIBarButtonItemStyleBordered target:self action:@selector(prevClicked:)]; 
    [buttons addObject:prevButton]; 
    [prevButton release]; 
    [toolbar setItems:buttons animated:YES]; 

    [self.view addSubview:toolbar]; 
    [toolbar release]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

回答

2

框架视图控制器的看法是不是最终的viewDidLoad方法,可能并不需要导航栏的大小成帐户 - 所以当显示导航栏时,最有可能您的工具栏不在最低点呃屏幕的边界。

要解决这个问题,您可以在viewWillAppear:方法中设置工具栏的框架(然后控制器的视图将具有正确的框架)。或尝试将工具栏的autoresizingMask属性设置为UIViewAutoresizingFlexibleTopMargin,以便工具栏“粘”到视图的底部。

+0

谢谢。当工具栏代码行进入viewWillAppear时,工作良好。 :d –