iOS 在 UITabBarController 中的 UIMoreNavigationController 中更改徽章颜色

cur*_*ckt 5 c# uitabbarcontroller uitabbaritem xamarin.ios

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

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);
Run Code Online (Sandbox Code Playgroud)

但我无法改变里面的徽章颜色UIMoreNavigationController

我试过这个:

this.MoreNavigationController.TopViewController.TabBarItem.BadgeColor = UIColor.White 
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

在里面也试过这个WillShowViewController

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

但仍然无法正常工作。

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

更多导航控制器


更新:

在调查了层次结构之后MoreNavigationController,显然PriorityDueBytab 的标记值被分配到了一个UILabel里面_UITableViewCellBadgeNeue。层次结构是:

  • this.MoreNavigationController.ViewControllers[0]: 这是一个 UIMoreListController
    • 获取View并将其转换为,UITableView因为那View是一个_UIMoreListTableView
      • 然后在该 tableview 内迭代VisibleCells,检查第四个IEnumerator对象中的和_UITableViewCellBadgeNeue
        • SubViews[0]里面_UITableViewCellBadgeNeueUILabel和标签的文字是徽章的价值。

基于此,我更改了标签TextColorBackgroundColorWillShowViewController. 它有效,但我需要在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++;
               }
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

Col*_*SFT 1

我重现了你的问题并找出了原因。

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

我的测试

查看层次结构WillShowViewController

UITableViewCellContentView
UIButton 
Run Code Online (Sandbox Code Playgroud)

查看层次结构DidShowViewController

UITableViewCellContentView
UIButton
_UITableViewCellBadgeNeue
_UITableViewCellSeparatorView
UITableViewCellContentView
UIButton
_UITableViewCellSeparatorView
Run Code Online (Sandbox Code Playgroud)

所以你只需要将 替换WillShowViewControllerDidShowViewController

PS:小建议

为了避免Apple改变View Hierarchy,可以使用like条件if (view.Class.Name.ToString() == "_UITableViewCellBadgeNeue")来代替判断是哪一层_UITableViewCellBadgeNeue