學習iOS自訂導航控制器UINavigationController_IOS

來源:互聯網
上載者:User

自訂導航控制器: 將導航控制器中通用的部分拿出來統一設定

1、一般導航條標題的字型setTitleTextAttribute和背景顏色setBackgroundImage都是統一的,可以在load方法中使用appearanceWhenContainedIn統一設定
2、一般導航條的返回按鈕需要自訂,一般除了棧底控制器有導航條,其他控制器都需要隱藏底部的條,可以重寫pushViewController:animated:方法,在該方法中實現該功能
3、導航控制器右滑返回效果(觸控螢幕幕的任意一點,向右滑動返回)

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];  // 螢幕邊緣滑動(只能在螢幕的邊緣才能觸發該手勢,不能在螢幕的任意一點觸發該手勢)  UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;  // 滑動手勢(禁用系統內建的螢幕邊緣滑動手勢,使用自訂的滑動手勢目的就是達到觸控螢幕幕上的任意一點向右滑動都能實現返回的效果)  UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:edgePanGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];  panGestureRecognizer.delegate = self;  [self.view addGestureRecognizer:panGestureRecognizer];  // 禁用系統的螢幕邊緣滑動手勢  edgePanGestureRecognizer.enabled = NO;}// 是否允許觸發手勢,如果是根視圖控制器則不需要- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {  return self.childViewControllers.count > 1;}- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {  // 統一設定返回按鈕  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

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.