Flutter/IOS页面混和开发,导航NavigationBar的显示/隐藏问题

在Flutter与IOS的混和开发中,页面混和是常见的情况,在此情况下,导航栏的显示隐藏是一个问题,单纯地设置hidden属性在页面跳转频繁情况下显得很冗余。每一次Flutter与IOS原生页面之间的跳转都需要处理Hidden.解决方法:UINavigationController提供了一套代理方法:

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController API_AVAILABLE(ios(7.0)) API_UNAVAILABLE(tvos);
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController API_AVAILABLE(ios(7.0)) API_UNAVAILABLE(tvos);

- (nullable id )navigationController:(UINavigationController *)navigationController
                          interactionControllerForAnimationController:(id ) animationController API_AVAILABLE(ios(7.0));

- (nullable id )navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC  API_AVAILABLE(ios(7.0));

在AppDelegate.m中,服从并设置UINavigationControllerDelegate代理:

@interface AppDelegate ()
 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:flutterMain];

    nav.delegate = self;

实现代理方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UIBarButtonItem *titleLessBack = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    viewController.navigationItem.backBarButtonItem = titleLessBack;
    viewController.navigationController.navigationBar.barTintColor = [UIColor systemBlueColor];
    viewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    viewController.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName ,nil];
    navigationController.navigationBar.hidden  = [viewController isKindOfClass:[MXContainerViewController class]];
    
}

在代理方法中:

判断是否为Flutter页面并隐藏NavigationBar:

 navigationController.navigationBar.hidden  = [viewController isKindOfClass:[XXXViewController class]];

其中XXXViewController为所有Flutter页面的的父控制器。

删除控制器返回键文字:

UIBarButtonItem *titleLessBack = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
   viewController.navigationItem.backBarButtonItem = titleLessBack;

你可能感兴趣的:(Flutter/IOS页面混和开发,导航NavigationBar的显示/隐藏问题)