2012-09-25 45 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 –