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

时间:2018-04-12 05:05:01

标签: 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.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

但仍无效。

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

More navigation controller

更新

在调查MoreNavigationController的层次结构后,显然PriorityDueBy标签的徽章价值已分配给UILabel内的_UITableViewCellBadgeNeue。层次结构是:

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

在此基础上,我更改了TextColor中的标签BackgroundColorWillShowViewController。它可以工作,但我需要在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++;
               }
          }
     }
}

1 个答案:

答案 0 :(得分:0)

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

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

我的测试

WillShowViewController

中查看层次结构
UITableViewCellContentView
UIButton 

DidShowViewController

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

因此,您只需将WillShowViewController替换为DidShowViewController

PS:小建议

为避免Apple更改View Hierarchy,您可以使用if (view.Class.Name.ToString() == "_UITableViewCellBadgeNeue")之类的条件,而不是判断_UITableViewCellBadgeNeue的级别。