遇到了iOS11 切换rootVC后之前的子控制器没有删除的问题。为了解决这个问题,采用了循环删除旧rootVC的子控制器的解决办法。但是这个办法会闪一下,为了解决这个问题,在切换之前做了截屏处理。
+ (void)replaceRootViewControllerTo:(UIViewController*) vc :(void(^)(void))callback {
AppDelegate* del = (AppDelegate*)[UIApplication sharedApplication].delegate;
UIViewController* root = del.window.rootViewController;
UIGraphicsBeginImageContextWithOptions(del.window.bounds.size, FALSE, UIScreen.mainScreen.scale);
[del.window drawViewHierarchyInRect:del.window.bounds afterScreenUpdates:YES];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView* view = [[UIImageView alloc] initWithImage:result];
[del.window addSubview:view];
[self dismissChildVCFrom:root :^{
del.window.rootViewController = vc;
//[del.window bringSubviewToFront:view];
[view removeFromSuperview];
if (callback) {
dispatch_async(dispatch_get_main_queue(), ^{
callback();
});
}
}];
}
+ (void)dismissChildVCFrom:(UIViewController *)vc :(void(^)(void))callback {
if ([vc presentedViewController]) {
UIViewController *nextRootVC = [vc presentedViewController];
[self dismissChildVCFrom:nextRootVC :^{
[nextRootVC dismissViewControllerAnimated:NO completion:callback];
}];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];
[self dismissChildVCFrom:nextRootVC :callback];
} else if ([vc isKindOfClass:[UINavigationController class]]){
UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
[self dismissChildVCFrom:nextRootVC :callback];
} else {
callback();
}
}