时间定位: 2021.4.1 愚人节快乐
最新版68的NavBar修改,与mPaaS文档中还是存在差异
自定义Plugin
- 自定义Plugin 继承NBPluginBase
@interface H5Plugin4NavigationBar: NBPluginBase
@end
- 监听事件
[self.target addEventListener:kNBEvent_Scene_NavigationItem_Left_Back_Create_After
withListener:self
useCapture:NO];
- 在对应事件中做处理
返回按钮
if ([kNBEvent_Scene_NavigationItem_Left_Back_Create_After isEqualToString:event.eventType]) {
//MARK: 更改已创建LeftBarItem的样式
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setImage:[UIImage imageNamed:@"header-back"] forState:UIControlStateNormal];
[btn setTitle:@" " forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:event.context.currentViewController action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
NSArray *leftBarButtonItems = event.context.currentViewController.navigationItem.leftBarButtonItems;
NSMutableArray *arr = [NSMutableArray arrayWithArray:leftBarButtonItems];
[arr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIBarButtonItem* _Nonnull item, NSUInteger idx, BOOL * _Nonnull stop) {
if ([item isKindOfClass:[AUBarButtonItem class]]) {
[arr removeObject:item];
[arr addObject:backItem];
*stop = YES;
}
}];
[event.context.currentViewController.navigationItem setLeftBarButtonItems:arr];
[event preventDefault];
}
关闭按钮,关闭按钮修改方式应该和返回一样,这边只是屏蔽关闭按钮
if ([kNBEvent_Scene_NavigationItem_Left_Close_Create_Before isEqualToString:event.eventType]) {
//MARK: 更改已创建LeftBarItem的样式
NBNavigationItemEvent *e = (id)event;
e.customView = nil;
[e preventDefault];
}
标题栏
文档中是这样的
if ([kNBEvent_Scene_TitleView_Create_Before isEqualToString:event.eventType]) {
// 重写 TitleView 的样式
NBNavigationTitleViewEvent *e = (id)event;
[e preventDefault];
}else if ([kNBEvent_Scene_TitleView_Create_After isEqualToString:event.eventType]) {
// 更改已创建 TitleView 的样式
NBNavigationTitleViewEvent *e = (id)event;
[[e.titleView mainTitleLabel] setFont:[UIFont systemFontOfSize:16]];
[[e.titleView mainTitleLabel] setTextColor:[UIColor greenColor]];
[e.titleView mainTitleLabel].lineBreakMode = NSLineBreakByTruncatingMiddle;
}
实际测试应该是下面这个
id <NBNavigationTitleViewProtocol> titleView = (NBNavigationTitleView *)self.navigationItem.titleView;
if ([titleView mainTitleLabel]) {
//这边增加了一个判断,防止重写titleView后,mainTitleLabel丢失
[[titleView mainTitleLabel] setFont:[UIFont systemFontOfSize:16]];
[[titleView mainTitleLabel] setTextColor:titleColor];
}