自定義導(dǎo)航控制器: 將導(dǎo)航控制器中通用的部分拿出來統(tǒng)一設(shè)置
1、一般導(dǎo)航條標(biāo)題的字體setTitleTextAttribute和背景顏色setBackgroundImage都是統(tǒng)一的,可以在load方法中使用appearanceWhenContainedIn統(tǒng)一設(shè)置
2、一般導(dǎo)航條的返回按鈕需要自定義,一般除了棧底控制器有導(dǎo)航條,其他控制器都需要隱藏底部的條,可以重寫pushViewController:animated:方法,在該方法中實(shí)現(xiàn)該功能
3、導(dǎo)航控制器右滑返回效果(觸摸屏幕的任意一點(diǎn),向右滑動(dòng)返回)
UIViewController
--navigationItem
leftBarButtonItem | leftBarButtonItems
NSString title | UIView titleView
rightBarButtonItem | rightBarButtonItems
backBarButtonItem
#import "BWNavigationController.h"#import "UIBarButtonItem+Item.h"@interface BaseNavigationController () <UIGestureRecognizerDelegate>@end@implementation BWNavigationController+ (void)load { [super load]; UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedIn:self, nil]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20]; [navigationBar setTitleTextAttributes:dict]; [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];}- (void)viewDidLoad { [super viewDidLoad]; // 屏幕邊緣滑動(dòng)(只能在屏幕的邊緣才能觸發(fā)該手勢,不能在屏幕的任意一點(diǎn)觸發(fā)該手勢) UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer; // 滑動(dòng)手勢(禁用系統(tǒng)自帶的屏幕邊緣滑動(dòng)手勢,使用自定義的滑動(dòng)手勢目的就是達(dá)到觸摸屏幕上的任意一點(diǎn)向右滑動(dòng)都能實(shí)現(xiàn)返回的效果) UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:edgePanGestureRecognizer.delegate action:@selector(handleNavigationTransition:)]; panGestureRecognizer.delegate = self; [self.view addGestureRecognizer:panGestureRecognizer]; // 禁用系統(tǒng)的屏幕邊緣滑動(dòng)手勢 edgePanGestureRecognizer.enabled = NO;}// 是否允許觸發(fā)手勢,如果是根視圖控制器則不需要- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return self.childViewControllers.count > 1;}- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { // 統(tǒng)一設(shè)置返回按鈕 NSInteger count = self.childViewControllers.count; if (count > 0) { viewController.hidesBottomBarWhenPushed = YES; viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backBarButtonItemWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highlightImage:[UIImage imageNamed:@"navigationButtonReturnClick"] tagert:self action:@selector(back) title:@"返回"]; } [super pushViewController:viewController animated:animated];}- (void)back { [self popViewControllerAnimated:YES];}@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選