Creating Navigation Bar programmatically with stor

2019-09-02 09:45发布

问题:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    UINavigationController *navVC = [UINavigationController new];
    ViewController *VC1 = [ViewController new];

    [window makeKeyAndVisible];

    [navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];

    [window setRootViewController:navVC];

    return window;
}

When calling this method, I get my Nav Bar but I don't see what i have in my ViewController view in the storyboard. I can't figure out what i'm doing wrong though, i've tried adding subview and stuff but nothing works out. Does anyone know what i am doing wrong?

回答1:

you need to write this code instead of what you have written

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UINavigationController *navVC = [UINavigationController new];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"your storyboard name" bundle:nil];

ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"];


[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];

 [self. window makeKeyAndVisible];

return YES;

}



回答2:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

UINavigationController *navVC = [UINavigationController new];
navVC.navigationBar.backgroundColor = [UIColor blackColor];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"];




 FirstViewController *VC2 = [mStoryboard instantiateViewControllerWithIdentifier:@"First"];
SecondViewController *VC3 = [mStoryboard instantiateViewControllerWithIdentifier:@"Second"];

[VC2.view addSubview:VC3.view];
 [navVC setViewControllers:@[VC1,VC2]animated:NO];

[self. window setRootViewController:navVC];

 [self. window makeKeyAndVisible];

return YES;

}