我正在使用objective-c
开发一个单视图应用程序,它的目标是11.4 iOS
,Xcode
版本是9.4.1。
创建后,有Main.storyboard和LaunchScreen.storyboard,我将Main.storyboard的背景改为黄色。
然后使用blow codesnip加载rootViewController
:
self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
当我运行应用程序时,应用程序的背景是黑色而不是黄色,有什么问题吗?
====添加截图:
====为init rootviewcontroller添加代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen alloc] bounds]];
self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
===我可以确认是didFinishLaunchingWithOptions中的代码失败了,如果我注释掉方法中的那些代码,一切都可以正常工作,不知道为什么。
发布于 2018-07-09 11:57:21
找到了原因,
initWithFrame:[[UIScreen alloc] bounds]
应该是
initWithFrame:[[UIScreen mainScreen] bounds]
发布于 2018-07-09 09:56:21
您不需要触发任何代码来获取Main.storyboard
初始视图控制器。
删除代码并转到Project settings -> General tab
,您将在此处找到Main Interface
,查看它是否设置为Main
(您的故事板名称)
还要检查storyboard
中的入口点是否设置为所需的viewcontroller
。
运行您的项目并查看。
发布于 2018-07-09 09:46:00
使用下面的代码在启动时获取视图控制器,
self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourViewController Storyboard ID."];
https://stackoverflow.com/questions/51242379
复制相似问题