All Products
Search
Document Center

Mobile Platform as a Service:Customize the navigation bar for HTML5 pages

Last Updated:Jan 17, 2023

Before customizing the navigation bar style for the HTML5 page, familiarize yourself with related knowledge about the framework navigation bar and HTML5 container, including:

Prerequisites

Before customizing the navigation bar style for the HTML5 page, familiarize yourself with related knowledge about the framework navigation bar and HTML5 container, including:

Set the default navigation bar style for all HTML5 pages

Given an app theme, to specify a uniform style for all HTML5 pages, you can customize a style by listening to the events provided by the Nebula container.

  • For details about events supported by the container, see the header file NebulaSDK/NBDefine.h.

    h5

  • The listening event of Customize a plugin needs to be customized:

      @implementation MPPlugin4TitleView
    
      - (void)pluginDidLoad
      {
          self.scope = kPSDScope_Scene;
          // -- Return area
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Left_Back_Create_Before withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Left_Back_Create_After withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Left_Close_Create_Before withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Left_Close_Create_After withListener:self useCapture:NO];
    
          // -- Title area
          [self.target addEventListener:kNBEvent_Scene_TitleView_Create_Before withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_TitleView_Create_After withListener:self useCapture:NO];
    
          // -- Control button area
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Right_Setting_Create_Before withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Right_Setting_Create_After withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Right_SubSetting_Create_After withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_NavigationItem_Right_Setting_Change withListener:self useCapture:NO];
    
          // -- Progress bar
          [self.target addEventListener:kNBEvent_Scene_ProgressView_Create_Before withListener:self useCapture:NO];
          [self.target addEventListener:kNBEvent_Scene_ProgressView_Create_After withListener:self useCapture:NO];
    
          [super pluginDidLoad];
      }
  • Set the styles of the Back button and Close button in the return area:

```
- (void)handleEvent:(PSDEvent *)event
{
    [super handleEvent:event];

    if ([kNBEvent_Scene_NavigationItem_Left_Back_Create_Before isEqualToString:event.eventType]) {
        [event preventDefault];

        event.context.currentViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(onClickBack)];
    }else if ([kNBEvent_Scene_NavigationItem_Left_Back_Create_After isEqualToString:event.eventType]){
        //Modify the style of the Back button.
        NSArray *leftBarButtonItems = event.context.currentViewController.navigationItem.leftBarButtonItems;
        if ([leftBarButtonItems count] == 1) {
            if (leftBarButtonItems[0] && [leftBarButtonItems[0] isKindOfClass:[AUBarButtonItem class]]) {
                // Based on the default Back button, modify the Back arrow and text color.
                AUBarButtonItem *backItem = leftBarButtonItems[0];
                backItem.backButtonColor = [UIColor greenColor];
                backItem.titleColor = [UIColor colorFromHexString:@"#00ff00"];

                // Hide the Back arrow.
//                backItem.hideBackButtonImage = YES;

                // Hide the Back text: Set the text to transparent and retain the s click area of the Back button.
//                backItem.titleColor = [UIColor clearColor];
        }
    }
}else if ([kNBEvent_Scene_NavigationItem_Left_Close_Create_Before isEqualToString:event.eventType]){
//        // Hide the Close button.
//        [event preventDefault];
//        NBNavigationItemLeftCloseEvent *itemEvent = (NBNavigationItemLeftCloseEvent *)event;
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(0, 0, 44, 44);
//        button.backgroundColor = [UIColor greenColor];
//        [button setTitle:@"Close" forState:UIControlStateNormal];
//        itemEvent.customView = button;
    }else if ([kNBEvent_Scene_NavigationItem_Left_Close_Create_After isEqualToString:event.eventType]){
//        // Modify the style of the Close button.
//        [event preventDefault];
//        NBNavigationItemLeftCloseEvent *itemEvent = (NBNavigationItemLeftCloseEvent *)event;
//        UIButton *closeButton = (UIButton *)itemEvent.customView;
//        [closeButton setTitle:@"Close" forState:UIControlStateNormal];
//        [closeButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    }
}
```
  • Set the title style of an HTML5 page:

if ([kNBEvent_Scene_TitleView_Create_Before isEqualToString:event.eventType]) {
        // Rewrite the style of TitleView.
        NBNavigationTitleViewEvent *e = (id)event;
        [e preventDefault];

    }else if ([kNBEvent_Scene_TitleView_Create_After isEqualToString:event.eventType]) {
        // Modify the created TitleView style.
        NBNavigationTitleViewEvent *e = (id)event;
        [[e.titleView mainTitleLabel] setFont:[UIFont systemFontOfSize:16]];
        [[e.titleView mainTitleLabel] setTextColor:[UIColor greenColor]];
        [e.titleView mainTitleLabel].lineBreakMode = NSLineBreakByTruncatingMiddle;
    }
  • Set the style of the OptionMenu control button:

if ([kNBEvent_Scene_NavigationItem_Right_Setting_Create_After isEqualToString:event.eventType] || [kNBEvent_Scene_NavigationItem_Right_SubSetting_Create_After isEqualToString:event.eventType]) {
        // Modify the created RightBarItem style.
        NBNavigationItemRightSettingEvent *settingEvent = (id)event;
        settingEvent.adjustsWidthToFitText = YES;
        settingEvent.maxWidth = [UIScreen mainScreen].bounds.size.width / 3.0f;
        UIButton *button = settingEvent.customView;
        button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        CGRect frame = CGRectMake(0, 0, 22, 22);
        button.frame = frame;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if (!CGSizeEqualToSize(button.bounds.size, frame.size)) {
            button.frame = frame;
        }
    }
  • Set the style of the progress bar displayed when an HTML5 page is loaded:

if([kNBEvent_Scene_ProgressView_Create_After isEqualToString:event.eventType]){
        NBProgressViewEvent *progressEvent = (NBProgressViewEvent *)event;
        id<NBProgressViewProtocol> progressView = progressEvent.progressView;
        [progressView setProgressTintColor:[UIColor greenColor]];
    }

Customize the navigation bar style of an HTML5 page

You can customize the navigation bar style of an HTML5 page before the page is loaded or after the page is opened.

  • Before the page is loaded: Perform the following steps to customize the navigation bar on the basis of the default navigation bar style.

    1. When the current HTML5 page is loaded, customize startup parameters and specify a customization method.

      • For details about transferring custom startup parameters for switching from one HTML5 page to another HTML5 page, see Open a new page and Start other apps.

      • For details about the method for transferring custom startup parameters for switching from a native page to an HTML5 page, see the following code:

      #pragma mark: the custom parameter to be passed when you open an HTML5 page. The base class of the HTML5 page will read the parameter to implement your settings of whether to hide the navigation pane.
       - (void)gotoHideNavigator
       {
           // Open an HTML5 page and hide the navigation bar.
           [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @"https://tech.antfin.com", @"showTitleBar":@NO,@"transparentTitle":@"auto"}];
       }
      
       - (void)gotoShowNavigator
       {
           // Open an HTML5 page and show the navigation bar.
           [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @"https://tech.antfin.com", @"showTitleBar":@YES}];
       }
      
       - (void)gotoTransparency
       {
           // Open an HTML5 page and set the navigation bar to transparent.
           [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @"https://tech.antfin.com", @"transparentTitle":@"auto"}];
       }
      
       - (void)gotoUpdateBackgroundColor
       {
           // Modify the background color of the navigation bar.
           [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @"https://tech.antfin.com", @"titleBarColor":@"16775138"}];
       }
      
       - (void)gotoUpdateStatusBarStyle
       {
           // Modify the color of the status bar.
           [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
       }
      
       - (void)gotoUpdateBackTitleColor
       {
           // Modify the default text and color of the Back button.
           [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @"https://tech.antfin.com", @"backButtonColor":@"ff0000"}];
       }
      
       - (void)gotoUpdateTitleColor
       {
           // Modify the title color.
           [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @"https://tech.antfin.com", @"titleColor":@"ff0000"}];
       }
    2. Handle the HTML5 base class. In the viewWillAppear method of the base class, call the native API method based on the transferred startup parameters to modify the navigation bar style.

      - (void)viewWillAppear:(BOOL)animated
      {
       [super viewWillAppear:animated];
      
       // WebView of the current page.
       UIWebView *webView = (UIWebView *)self.psdContentView;
       NSLog(@"[mpaas] webView: %@", webView);
      
       // Startup parameters of the current page.
       NSDictionary *expandParams = self.psdScene.createParam.expandParams;
       NSLog(@"[mpaas] expandParams: %@", expandParams);
      
       if ([expandParams count] > 0) {
           [self customNavigationBarWithParams:expandParams];
       }
       WKWebView *webView = (WKWebView *)[self psdContentView];
       if (@available(iOS 11.0, *)) {
         webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
       } else {
         self.automaticallyAdjustsScrollViewInsets = NO;
       }
       webView.scrollView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
       webView.scrollView.scrollIndicatorInsets = webView.scrollView.contentInset;
      }
      
      - (void)customNavigationBarWithParams:(NSDictionary *)expandParams
      {
       // Customize the background color of the navigation bar.
       NSString *titleBarColorString = expandParams[@"titleBarColor"];
       if ([titleBarColorString isKindOfClass:[NSString class]] && [titleBarColorString length] > 0) {
           UIColor *titleBarColor = [UIColor colorFromHexString:titleBarColorString];
           [self.navigationController.navigationBar setNavigationBarStyleWithColor:titleBarColor translucent:NO];
           [self.navigationController.navigationBar setNavigationBarBottomLineColor:titleBarColor];
       }
      
       // Set whether to hide the navigation bar, which is not hidden by default. After the navigation pane is set to be hidden, the WebView needs to be displayed in full-screen mode.
       NSString *showTitleBar = expandParams[@"showTitleBar"];
       if (showTitleBar && ![showTitleBar boolValue]) {
       self.options.showTitleBar = NO;
           [self.navigationController setNavigationBarHidden:YES];
      
           // Adjust the position of the WebView.
           UIWebView *webView = (UIWebView *)[self psdContentView];
           CGRect frame = webView.frame;
           frame.origin.y = [[UIApplication sharedApplication] statusBarFrame].size.height;
           frame.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height;
           webView.frame = frame;
           self.automaticallyAdjustsScrollViewInsets = NO;
      
       }
      
       // Set whether the navigation bar is transparent. The navigation bar is not transparent by default. After the navigation pane is set to transparent, the WebView needs to be displayed in full-screen mode.
       NSString *transparentTitle = expandParams[@"transparentTitle"];
       if ([transparentTitle isEqualToString:@"always"] || [transparentTitle isEqualToString:@"auto"]) {
      
           // The navigation bar and bottom line become transparent.
           UIColor *clearColor = [UIColor clearColor] ;
           [self.navigationController.navigationBar setNavigationBarTranslucentStyle];
           [self.navigationController.navigationBar setNavigationBarStyleWithColor:clearColor translucent:YES];
      
           // Adjust the position of the WebView.
           self.edgesForExtendedLayout = UIRectEdgeAll;
           if (@available(iOS 11.0, *)) {
               UIWebView *wb = (UIWebView *)[self psdContentView];
               wb.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
           }else{
               self.automaticallyAdjustsScrollViewInsets = NO;
           }
       }
      
       // Modify the default text and color of the Back button.
       NSString *backButtonColorString = expandParams[@"backButtonColor"];
       if ([backButtonColorString isKindOfClass:[NSString class]] && [backButtonColorString length] > 0) {
           UIColor *backButtonColor = [UIColor colorFromHexString:backButtonColorString];
      
           NSArray *leftBarButtonItems = self.navigationItem.leftBarButtonItems;
           if ([leftBarButtonItems count] == 1) {
               if (leftBarButtonItems[0] && [leftBarButtonItems[0] isKindOfClass:[AUBarButtonItem class]]) {
                   AUBarButtonItem *backItem = leftBarButtonItems[0];
                   backItem.titleColor = backButtonColor;
                   backItem.backButtonColor = backButtonColor;
               }
           }
       }
      
       // Set the title color.
       NSString *titleColorString = expandParams[@"titleColor"];
       if ([titleColorString isKindOfClass:[NSString class]] && [titleColorString length] > 0) {
           UIColor *titleColor = [UIColor colorFromHexString:titleColorString];
           id<NBNavigationTitleViewProtocol> titleView = self.navigationItem.titleView;
           [[titleView mainTitleLabel] setFont:[UIFont systemFontOfSize:16]];
           [[titleView mainTitleLabel] setTextColor:titleColor];
       }
      }
  • After the page is opened: Modify the navigation bar style dynamically during user operation. Call the native API method to modify the style through a custom JSAPI.

    • Customize a JSAPI to handle the navigation bar style of the current page.

    • Handle the native navigation bar in the JSAPI based on the API provided in Customize the navigation bar style of an HTML5 page.

       - (void)handler:(NSDictionary *)data context:(PSDContext *)context callback:  (PSDJsApiResponseCallbackBlock)callback
        {
        [super handler:data context:context callback:callback];
      
        UIViewController *currentVC = context.currentViewController;
        currentVC.navigationItem.titleView = [[AUDoubleTitleView alloc] initWithTitle:@"Title" detailTitle:@"Subtitle"];
        callback(@{@"success":@YES});
        }