自定义UINavigationController导航控制器实现传统的框架(二)

首先自定义一个QBYNavigationController类继承UINavigationController。接下来就可以在类中做以下几件事情:

  • 1,修改导航控制器的标题文字大小,背景图片,因为是一次性修改,所以还是在load方法中:

objc

  • (void)load{

    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];

    // 只要是通过模型设置,都是通过富文本设置
    // 设置导航条标题 => UINavigationBar
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    [navBar setTitleTextAttributes:attrs];

    // 设置导航条背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];

}

- 2,返回按钮的实现,其中也包括了隐藏tabBar:

```objc```
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{

    if (self.childViewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithimage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(back) title:@"返回"];
    }
//    正真跳转
    [super pushViewController:viewController animated:animated];
}

- (void)back{
    [self popViewControllerAnimated:YES];
}

3,如果要返回手势的话实现协议,添加下面代码即可:

objc

  • (void)viewDidLoad{
    [super viewDidLoad];

    self.interactivePopGestureRecognizer.delegate = self;
    }

pragma mark - UIGestureRecognizerDelegate

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    return self.childViewControllers.count > 1;
    }

你可能感兴趣的:(自定义UINavigationController导航控制器实现传统的框架(二))