Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push it to the next page and hide the tabBar and pop it back and show the tabBar, and the tableView content moves down #102

Open
myiosemail opened this issue Oct 11, 2019 · 1 comment

Comments

@myiosemail
Copy link

Description

[Description of the issue]

Steps to Reproduce

[Steps or usage snippet reproducing the issue]

  1. [First Step]
  2. [Second Step]
  3. [and so on...]
<snippet>

Expected behavior: [What you expect to happen]
Actual behavior: [What actually happens]
Reproduces how often: [What percentage of the time does it reproduce?]

System configuration

Xcode version:
iOS version:
Affected device:

@Andy0570
Copy link

English is not my native language; please excuse typing errors.

I solved this problem by adding extension methods to UIViewController.
Inspired By https://github.com/Coding/Coding_iOS/Util/OC_Category/UIViewController+Swizzle.h

UIViewController+Swizzle.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (Swizzle)

@end

NS_ASSUME_NONNULL_END

UIViewController+Swizzle.m

#import "UIViewController+Swizzle.h"
#import "RDVTabBarController.h"
#import <objc/runtime.h>

@implementation UIViewController (Swizzle)

// 视图控制器将要显示,如果该视图控制器不是 nav 的根视图控制器,则隐藏 tabBar
- (void)customViewWillAppear:(BOOL)animated {
    if (self.navigationController.childViewControllers.count > 1) {
        [self. rdv_tabBarController setTabBarHidden:YES animated:YES];
    }
    [self customViewWillAppear:animated];
}

// 视图控制器已经显示,如果该视图控制器是 nav 的根视图控制器,则显示 tabBar
- (void)customViewDidAppear:(BOOL)animated {
    if (self.navigationController.childViewControllers.count == 1) {
        [self. rdv_tabBarController setTabBarHidden:NO animated:YES];
    }
    [self customViewDidAppear:animated];
}

// 视图控制器将要消失
- (void)customViewWillDisappear:(BOOL)animated {
    // 设置返回按钮 (backBarButtonItem 的图片不能设置;如果用 leftBarButtonItem 属性,则 iOS7 自带的滑动返回功能会失效)
    if (!self.navigationItem.backBarButtonItem && self.navigationController.viewControllers.count > 1) {
        self.navigationItem.backBarButtonItem = [self backButton];
    }
    [self customViewWillDisappear:animated];
}

#pragma mark - BackButton

- (UIBarButtonItem *)backButton {
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = NSLocalizedString(@"back", nil);
    temporaryBarButtonItem.target = self;
    temporaryBarButtonItem.action = @selector(goBack_Swizzle);
    return temporaryBarButtonItem;
}

- (void)goBack_Swizzle {
    [self.navigationController popViewControllerAnimated:YES];
}

+ (void)load {
    Class class = [UIViewController class];
    
    Method originMethod = class_getInstanceMethod(class, @selector(viewWillAppear:));
    Method newMethod = class_getInstanceMethod(class, @selector(customViewWillAppear:));
    method_exchangeImplementations(originMethod, newMethod);
    
    originMethod = class_getInstanceMethod(class, @selector(viewDidAppear:));
    newMethod = class_getInstanceMethod(class, @selector(customViewDidAppear:));
    method_exchangeImplementations(originMethod, newMethod);
    
    originMethod = class_getInstanceMethod(class, @selector(viewWillDisappear:));
    newMethod = class_getInstanceMethod(class, @selector(customViewWillDisappear:));
    method_exchangeImplementations(originMethod, newMethod);
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants