我正在尝试使用以下代码在首次启动时为我的应用程序提供一个教程/登录/介绍视图控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *firstController = [[UIViewController alloc] init];
[self.window setRootViewController:firstController];
if ([self shouldShowIntro]) {
IntroViewController *introViewController = [[IntroViewController alloc] init];
[firstController presentViewController:introViewController animated:NO completion:nil];
}
return YES;
}
这可以正常工作,但是会产生令人讨厌的视觉效果...在您看到
IntroViewController
之前,有一瞬间可以看到firstController
。我尝试在设置窗口的rootViewController之前呈现IntroViewController
,但这(不足为奇)会导致以下警告:Warning: Attempt to present <IntroViewController: 0x7fd8eb3362f0> on <UIViewController: 0x7fd8eb335180> whose view is not in the window hierarchy!
如何在没有这种烦人的视觉效果的情况下模态显示IntroViewController?我希望
IntroViewController
在启动屏幕消失后已经可以显示并且可以模态关闭。 最佳答案
以下内容适用于我,并且在iPhone 6上的iOS 8.1和模拟器中均无闪烁。我使用的是SDK版本8.1,我认为您的SDK级别有所不同,因为与您使用提供的代码相比,我得到的警告和结果不同。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *firstController = [[UIViewController alloc] init];
firstController.view.backgroundColor = [UIColor blueColor];
[self.window setRootViewController:firstController];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController* modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor grayColor];
[firstController presentViewController: modalViewController animated: NO completion: nil];
});
// dismiss after a few seconds..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[firstController dismissViewControllerAnimated: YES completion: nil];
});
return YES;
}
使用MMDrawerController更新:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MMDrawerController *drawerController = [[MMDrawerController alloc] init];
drawerController.centerViewController = [[UIViewController alloc] init];
drawerController.centerViewController.view.backgroundColor = [UIColor blueColor];
drawerController.rightDrawerViewController = [[UIViewController alloc] init];
drawerController.rightDrawerViewController.view.backgroundColor = [UIColor greenColor];
drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
drawerController.closeDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
[self.window setRootViewController:drawerController];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController* modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor grayColor];
[drawerController presentViewController: modalViewController animated: NO completion: nil];
});
// dismiss after a few seconds..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[drawerController dismissViewControllerAnimated: YES completion: nil];
});
return YES;
}
另外,根据您的评论,听起来好像拥有预加载的视图是关键。在呈现之前,请尝试调用[firstController视图]和[modalController视图]以确保已加载它们。