Presenting a Modal View Controller hides the Navigation Bar

后端 未结 9 1022
灰色年华
灰色年华 2021-01-04 00:58

I have a navigation based app with a navigation bar, but there are a few instances where instead of pushing a view controller onto the stack, I need to present the view cont

9条回答
  •  醉酒成梦
    2021-01-04 01:30

    Xcode has a template that does pretty close to what you're doing. from the results, i don't think you should be attempting to perform [self.navigationController presentModalViewController:vc] and [self.navigationController dismissModalViewControllerAnimated:] , but rather simply [self presentModalViewController:] and [self dismissModalViewControllerAnimated:] .

    to see how the template does this for yourself, you can use the new project wizard in xcode 4.3 . perhaps it will provide some guidance:

    Xcode new project wizard utility template

    from that choice, choose Next, then give your test project a name, choose "Universal", turn off automatic reference counting, hit next, save where you want it.

    now, click on the target and switch the deployment target to 4.3 (or 4.0 if you prefer) for your testing purposes, and switch to your device or the iOS 4.3 simulator .

    finally, substitute the following code in applicationDidFinishLaunching:withOptions: in the created AppDelegate.m:

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPhone"
                                                                            bundle:nil] autorelease];
        } else {
            self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPad"
                                                                            bundle:nil] autorelease];
        }
        UINavigationController* navigationController
          = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
        return YES;
    

    now, when i run this, it doesn't hide the navigationBar. and in the created MainViewController.m from the template, you'll see how it presents the modal view controller and dismisses it from the controller itself and not from the navigation controller. for good measure, to make the template code more like your own, go into MainViewController.m and delete the line that sets the modal view controller transition style ...

    (of course, in iOS 5, with storyboards, the same thing can all be accomplished with modal segues ... which is how i've done this for apps that i'm not supporting for pre-5.0 that present a modalViewController in this fashion.)

提交回复
热议问题