欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

学习iOS自定义导航控制器UINavigationController

程序员文章站 2023-12-17 21:33:04
自定义导航控制器: 将导航控制器中通用的部分拿出来统一设置 1、一般导航条标题的字体settitletextattribute和背景颜色setbackgroundimag...

自定义导航控制器: 将导航控制器中通用的部分拿出来统一设置

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: