mPaaS 10.1.60 H5容器中NavBar修改的问题

时间定位: 2019.12.23
升级60-beta之后,发现10.1.32中对navBar做的修改,比如返回按钮,右边按钮做的自定义修改失效了,经过和mPaaS沟通,总结修改方式如下:

自定义Plugin

  1. 自定义Plugin 继承NBPluginBase
@interface H5Plugin4NavigationBar: NBPluginBase
@end
  1. 监听事件
    [self.target addEventListener:kNBEvent_Scene_NavigationItem_Left_Back_Create_After
                     withListener:self
                       useCapture:NO];
  1. 在对应事件中做处理

返回按钮

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];
    }

标题栏

以前是这样的

//10.1.32 中标题栏的修改方式是这样的,60-beta中改了
if ([kNBEvent_Scene_TitleView_Create_After isEqualToString:event.eventType]) {
        //MARK: 更改已创建TitleView的样式
        NBNavigationTitleViewEvent *e = (id)event;
        [[e.titleView mainTitleLabel] setFont:[UIFont systemFontOfSize:18]];
        [[e.titleView mainTitleLabel] setTextColor:[UIColor blackColor]];
    }

60-beta中是下面这个样子

    id  titleView = (NBNavigationTitleView *)self.navigationItem.titleView;
    [[titleView mainTitleLabel] setFont:[UIFont systemFontOfSize:9]];
    [[titleView mainTitleLabel] setTextColor:[UIColor redColor]];

右按钮

    BOOL isNebulaApp = [expandParams[@"isNebulaApp"] isEqualToString:@"1"];
  //这边的isNebulaApp用来控制入口是小程序还是普通h5APP,mPaaS未提供容器内判断是小程序还是H5包的方法,只能通过启动时传参来判断
    UIBarButtonItem * rightItem = self.navigationItem.rightBarButtonItems.firstObject;
    if (isNebulaApp && rightItem) {
        UIButton * rightButton = (UIButton *)rightItem.customView;
        [rightButton setBackgroundImage:[UIImage imageNamed:@"shouye-hei"] forState:(UIControlStateNormal)];
    }

你可能感兴趣的:(mPaaS 10.1.60 H5容器中NavBar修改的问题)