- First assign
storyboard id
to the abcViewController in storyboard eg."firstView". - Import the viewController in app delegate
- In the storyboard, uncheck the "Is initial View Controller" attribute from the first view controller.
- In the app's
info.plist
, remove the value of "Main storyboard file base name". Instantiate the storyboard, create the window object and set initial view controller in the app delegate's
application:didFinishLaunchingWithOptions:
method- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; TestViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"firstView"]; UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)]; [redView setBackgroundColor:[UIColor redColor]]; UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)]; [greenView setBackgroundColor:[UIColor greenColor]]; [redView addSubview:greenView]; [controller.view addSubview:redView]; self.window.rootViewController = controller; [self.window makeKeyAndVisible]; return YES; }
adding UIViewcontroller's view to UIWindow does not show
-
03-07-2022 - |
Question
I am trying to see a view with the following code where I am using storyboard however I did not make the uiviewcontroller's scene initial view controller. This is the code I wrote in didFinishLaunchingWithOption of my AppDelegate.
UIWindow* window = [UIApplication sharedApplication].keyWindow;
abcViewController *controller = [[abcViewController alloc]init];
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)];
[redView setBackgroundColor:[UIColor redColor]];
UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)];
[greenView setBackgroundColor:[UIColor greenColor]];
[redView addSubview:greenView];
[controller.view addSubview:redView];
window.rootViewController = controller;
[window makeKeyAndVisible];
return YES;
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow