### 导航栏NavigationBar的常见问题

[TOC]

目录

自定义导航栏按钮

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];leftBtn.frame = CGRectMake(0, 0, 25,25);
  [leftBtn setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
  [leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}

- (void)leftBarBtnClicked:(UIButton *)btn{ 
  [self.navigationController popViewControllerAnimated:YES];
}

调整导航栏按钮位置

//创建返回按钮
UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(0, 0, 25,25);
[leftBtn setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];[leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
//创建UIBarButtonSystemItemFixedSpace
UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//将宽度设为负值
spaceItem.width = -15;
//将两个BarButtonItem都返回给NavigationItem
self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];

自定义返回按钮后滑动返回手势失效


self.navigationController.interactivePopGestureRecognizer.delegate = self;

全屏滑动返回手势


- (void)viewDidLoad{
  [super viewDidLoad];
  // 获取系统自带滑动手势的target对象
  id target = self.interactivePopGestureRecognizer.delegate;
  // 创建全屏滑动手势,调用系统自带滑动手势的target的action方法
  // handleNavigationTransition系统滑动方法 不会因为私有API被拒
  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
  // 设置手势代理,拦截手势触发
  pan.delegate = self;
  // 给导航控制器的view添加全屏滑动手势
  [self.view addGestureRecognizer:pan];
  // 禁止使用系统自带的滑动手势
  self.interactivePopGestureRecognizer.enabled = NO;
}

导航栏NavigationBar切换动画

1. FDFullscreenPopGesture

FDFullscreenPopGesture简单粗暴好用

2.KLTAnimateNav

KLTAnimateNav写的不错,内容比较简单有学习价值

你可能感兴趣的:(### 导航栏NavigationBar的常见问题)