IQKeyboardManager 在iOS11导航栏消失的问题

戴正阳
2023-12-01

问题原因:IQKeyboardManager 在获取当前顶层控制器的时候获取到了tabBarController

解决办法:

找到IQUIView_Hierarchy.m 修改-(UIViewController *)topMostController方法

-(UIViewController )topMostController
{
NSMutableArray<UIViewController
> *controllersHierarchy = [[NSMutableArray alloc] init];

UIViewController *topController = self.window.rootViewController;

if (topController)
{
    [controllersHierarchy addObject:topController];
}

while ([topController presentedViewController]) {
    
    topController = [topController presentedViewController];
    [controllersHierarchy addObject:topController];
}

UIViewController *matchController = [self viewController];

while (matchController != nil && [controllersHierarchy containsObject:matchController] == NO)
{
    do
    {
        matchController = (UIViewController*)[matchController nextResponder];
        
    } while (matchController != nil && [matchController isKindOfClass:[UIViewController class]] == NO);
}

return (UIViewController*)matchController;

}

-(UIViewController *)topMostController{

UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;

if([rootController isKindOfClass:[UITabBarController class]]){
    UITabBarController *tabBarController = (UITabBarController *)rootController;
    UINavigationController *selectController = tabBarController.selectedViewController;
    UIViewController *viewController = (UIViewController *)selectController.visibleViewController;
    while (viewController.presentedViewController) {
        viewController = (UIViewController *)viewController.presentedViewController;
    }
    
    return viewController;
}else if ([rootController isKindOfClass:[UINavigationController class]]) {
    UINavigationController *selectController = (UINavigationController *)rootController;
    return selectController.visibleViewController;
}else if ([rootController isKindOfClass:[UIViewController class]]) {
    return rootController;
}  
else{
    return nil;
}

}

 类似资料: