C# iOS更改UIAbbarController中UIMoreNavigationController内的徽章颜色

C# iOS更改UIAbbarController中UIMoreNavigationController内的徽章颜色,c#,xamarin.ios,uitabbarcontroller,uitabbaritem,C#,Xamarin.ios,Uitabbarcontroller,Uitabbaritem,我使用以下代码自定义背景色及其色调: var blackBGColor = new UIColor(new nfloat(40) / 255f, new nfloat(40) / 255f, new nfloat(40) / 255f, 1f); this.MoreNavigationController.TopViewController.View.Background

我使用以下代码自定义背景色及其色调:

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);
但是我无法更改
UIMoreNavigationController
中的徽章颜色

我试过这个:

this.MoreNavigationController.TopViewController.TabBarItem.BadgeColor = UIColor.White 
但它不起作用

在WillShowViewController中也尝试了此项:

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

但仍然不起作用

有没有办法改变徽章的颜色


更新:

在调查了
MoreNavigationController
的层次结构后,
优先级
DueBy
选项卡的徽章值显然被分配给
内的
UILabel
。层次结构是:

  • this.MoreNavigationController.ViewController[0]
    :这是一个
    UIMoreListController
    • 获取
      视图
      并将其强制转换为
      UITableView
      ,因为
      视图
      是一个
      \UIMoreListTableView
      • 然后在tableview
        VisibleCells
        中迭代,检查
        IEnumerator
        ,在第四个对象中有
        \uUITableViewCellBadgeneue
        • \u UITableViewCellBadgeNeue
          内部的
          子视图[0]
          是一个
          ui标签
          ,标签的文本是徽章值
基于此,我更改了
WillShowViewController
中的标签
TextColor
BackgroundColor
。它可以工作,但我需要从
Priority/DueBy
选项卡来回切换到
More
选项卡。第一次就不行了

[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++;
               }
          }
     }
}

我复制了你的问题并找出了原因

这是因为在
WillShowViewController
中检索视图层次时,TableView尚未完成渲染(绘制)

我的测试 查看
中的层次结构将显示ViewController

UITableViewCellContentView
UIButton 
UITableViewCellContentView
UIButton
_UITableViewCellBadgeNeue
_UITableViewCellSeparatorView
UITableViewCellContentView
UIButton
_UITableViewCellSeparatorView
查看
DidShowViewController中的层次结构

UITableViewCellContentView
UIButton 
UITableViewCellContentView
UIButton
_UITableViewCellBadgeNeue
_UITableViewCellSeparatorView
UITableViewCellContentView
UIButton
_UITableViewCellSeparatorView
因此,您只需将
WillShowViewController
替换为
DidShowViewController

附言:小建议
为了避免Apple更改视图层次结构,您可以使用
if(View.Class.Name.ToString()==“\u UITableViewCellBadgeNeue”)
这样的条件,而不是判断
\u UITableViewCellBadgeNeue
是哪个级别。我在TabbedRenderer中搜索了源代码,但没有找到任何东西。2.我在Xcode中使用
Debug View Hierarchy
来查找MoreNavigationController中的徽章视图,它被称为
\u UITableViewCellBadgeNeue
,但不幸的是,我们无法访问它。因此,我认为唯一的方法是自定义tableview或tableviewCell,而不是原来的。@ColeXia MSFT我已尝试在
中更改
的UITableViewCellBadgeNeue
中的颜色。但是,它只有在我从“更多”选项卡到“优先级/到期日”选项卡来回(至少需要2-3次)后才起作用。第一次它永远不会工作。你能附加代码来帮助我们复制吗?@ColeXia MSFT我已经添加了代码如果我将代码放入
DidShowViewController
,它将以灰色和白色背景色显示徽章值,然后在几秒钟后,它将变为白色和清晰的背景色。@currapickt是的,这是一个问题,但这比推到下一个viewController并返回要好…还没有解决它。即使我将代码放入
DidShowViewController
,有时它也不起作用(这是随机的,无法预测)。如果发生这种情况,我需要在控制器之间来回切换。那么人们在做什么来修复这个问题呢?我有一个类似的问题,我正试图解决黑暗背景下的问题。