S.J. Lim
S.J. Lim

Reputation: 3165

Why UIToolBar is disappeared in NavigationController?

I'm trying to push ViewController (Next, call ViewCon) to NavigationController. (Next, call NavCon)

But when ViewCon is pushed to NavCon, ViewCon's toolbar is disappeared.

ViewCon's toolbar well work in when ViewCon is not pushed in NavCon.

Who know the reason?

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

Upvotes: 1

Views: 430

Answers (1)

Vladimir
Vladimir

Reputation: 170829

Frame of view controller's view is not final in viewDidLoad method and probably does not takes navigationbar size into account - so when navigation bar is displayed most likely your toolbar goes out of the lower boundaries of the screen.

To fix that you can set toolbar's frame in the viewWillAppear: method (controller's view will have correct frame then). Or try to set toolbar's autoresizingMask property to UIViewAutoresizingFlexibleTopMargin so that toolbar will "stick" to the view's bottom.

Upvotes: 2

Related Questions