自定義UINavigationController導航控制器實現傳統的框架(二)

weixin_34377065發表於2017-07-21

首先自定義一個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,如果要返回手勢的話實現協議<UIGestureRecognizerDelegate>,新增下面程式碼即可:

objc

  • (void)viewDidLoad{
    [super viewDidLoad];

    self.interactivePopGestureRecognizer.delegate = self;
    }

pragma mark - UIGestureRecognizerDelegate

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

    return self.childViewControllers.count > 1;
    }

相關文章