5

I customize the background color and its tint color with this code:

var blackBGColor = new UIColor(new nfloat(40) / 255f,
                               new nfloat(40) / 255f,
                               new nfloat(40) / 255f, 1f);

this.MoreNavigationController.TopViewController.View.BackgroundColor = blackBGColor;

var moreTableView = (UITableView)this.MoreNavigationController.TopViewController.View;
moreTableView.TintColor = UIColor.White;
foreach (var cell in moreTableView.VisibleCells)
{
     cell.BackgroundColor = blackBGColor;
     cell.TextLabel.TextColor = UIColor.White;
     var selectedView = new UIView
     {
          BackgroundColor = UIColor.DarkGray
     };
     cell.SelectedBackgroundView = selectedView;
}

this.MoreNavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
this.MoreNavigationController.NavigationBar.Translucent = false;
this.MoreNavigationController.NavigationBar.TintColor = UIColor.White;
this.MoreNavigationController.NavigationBar.BarTintColor = new UIColor(24f / 255f, 24f / 255f, 24f / 255f, 1f);

But I couldn't change the badge color inside UIMoreNavigationController.

I've tried this:

this.MoreNavigationController.TopViewController.TabBarItem.BadgeColor = UIColor.White 

but it's not working.

Tried this one too inside WillShowViewController:

this.ViewControllers[4].TabBarItem.BadgeColor = UIColor.White

but still not working.

Is there any way to change the badge color?

More navigation controller


UPDATE:

After investigating the hierarchy of MoreNavigationController, apparently the badge value for Priority and DueBy tab is assign to a UILabel inside _UITableViewCellBadgeNeue. The hierarchy is:

  • this.MoreNavigationController.ViewControllers[0]: this is a UIMoreListController
    • Get the View and cast it to UITableView because that View is a _UIMoreListTableView
      • Then iterate inside that tableview VisibleCells, check the IEnumerator and in the forth object there is _UITableViewCellBadgeNeue
        • The SubViews[0] inside _UITableViewCellBadgeNeue is a UILabel and the label's text is the badge value.

Based on that, I change the label TextColor and BackgroundColor in WillShowViewController. It works but I need to go back and forth from Priority/DueBy tab to More tab. It never works on the first time.

[Export("navigationController:willShowViewController:animated:")]
public void WillShowViewController(UINavigationController navigationController, UIViewController viewController, bool animated)
{
     if (this.MoreNavigationController.ViewControllers.Contains(viewController))
     {
          this.ViewControllers[4].TabBarItem.BadgeValue = this.ViewModel.SelectedPrioritiesFilter.Count > 0 ? this.ViewModel.SelectedPrioritiesFilter.Count.ToString() : null;
          this.ViewControllers[5].TabBarItem.BadgeValue = this.ViewModel.SelectedDueByFilter != null ? "1" : null;

          //this is the code to change the color
          var vc = this.MoreNavigationController.ViewControllers[0];
          var moreTableView = (UITableView)vc.View;
          foreach (var cell in moreTableView.VisibleCells)
          {
               var enumerator = cell.GetEnumerator();
               var i = 0;
               while (enumerator.MoveNext())
               {
                    //_UITableViewCellBadgeNeue is in the forth object
                    if(i == 3) 
                    {
                        //_UITableViewCellBadgeNeue is a UIView
                        if (enumerator.Current is UIView)
                        {
                             var current = (UIView)enumerator.Current;
                             if (current != null)
                             {
                                 if (current.Subviews.Length > 0)
                                 {
                                      var label = (UILabel)current.Subviews[0];
                                      label.TextColor = UIColor.White;
                                      label.BackgroundColor = UIColor.Clear;
                                 }
                             }
                        }
                    }
                    i++;
               }
          }
     }
}
currarpickt
  • 2,290
  • 4
  • 24
  • 39
  • 1. I searched source code in TabbedRenderer but didn't find anything. 2. I use `Debug View Hierarchy` with Xcode to find badge view inside moreNavigationContoller ,it is called `_UITableViewCellBadgeNeue` , but unfortunately we cannot be able to access it .So I think the only way is to customize the tableview or tableviewCell instead of the original one. – ColeX Apr 18 '18 at 08:13
  • @ColeXia-MSFT I've tried to change the color in `_UITableViewCellBadgeNeue` in `WillShowViewController`. But, it only works after I go back and forth (at least need 2-3 times) from more tab to priority/due by tab. It never works for the first time. – currarpickt Apr 19 '18 at 03:33
  • Could you attach the code to help us reproduce? – ColeX Apr 19 '18 at 03:35
  • @ColeXia-MSFT I've added the code – currarpickt Apr 19 '18 at 04:24

1 Answers1

1

I reproduced your issue and figured the reason out.

It is because the TableView has not finished rendering(drawing) when you retrieve the view hierarchy in WillShowViewController.

My test

View Hierarchy in WillShowViewController

UITableViewCellContentView
UIButton 

View Hierarchy in DidShowViewController

UITableViewCellContentView
UIButton
_UITableViewCellBadgeNeue
_UITableViewCellSeparatorView
UITableViewCellContentView
UIButton
_UITableViewCellSeparatorView

So you just need replace the WillShowViewController with DidShowViewController.

PS: Small Suggestion

To avoid the change of View Hierarchy by Apple, you can use the condition like if (view.Class.Name.ToString() == "_UITableViewCellBadgeNeue") instead of judging which level the _UITableViewCellBadgeNeue is.

Community
  • 1
  • 1
ColeX
  • 14,062
  • 5
  • 43
  • 240
  • if I put the code in `DidShowViewController`, it will show the badge value in gray and white background color then in several seconds, it changes to white and clear bg color. – currarpickt Apr 19 '18 at 06:45
  • @currarpickt yes, this is a problem, but it is better than pushing to next viewController and back... – ColeX Apr 19 '18 at 06:49
  • haven't solve it. even if I put the code in `DidShowViewController`, sometimes it doesn't work (and this is random, can't predict it). I need to go back and forth again between the controllers if that's happen. – currarpickt Apr 23 '18 at 03:56
  • So what are folks doing to fix this? I have a similar issue with a dark background I'm trying to solve for. – Mavro Oct 29 '18 at 02:08