【问题标题】:iOS Change badge color inside UIMoreNavigationController in UITabBarControlleriOS在UITabBarController中的UIMoreNavigationController中更改徽章颜色
【发布时间】:2018-04-12 05:05:00
【问题描述】:

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

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 的层次结构后,显然PriorityDueBy 选项卡的徽章值分配给_UITableViewCellBadgeNeue 内的UILabel。层次结构是:

  • this.MoreNavigationController.ViewControllers[0]:这是UIMoreListController
    • 获取View 并将其转换为UITableView,因为View_UIMoreListTableView
      • 然后在该表视图VisibleCells 中进行迭代,检查IEnumerator,在第四个对象中有_UITableViewCellBadgeNeue
        • _UITableViewCellBadgeNeue 中的 SubViews[0]UILabel,标签的文本是徽章值。

基于此,我在WillShowViewController 中更改标签TextColorBackgroundColor。它有效,但我需要从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.我在 TabbedRenderer 中搜索了源代码,但没有找到任何东西。 2.我用Debug View Hierarchy和Xcode在moreNavigationContoller里面找到badge view,叫做_UITableViewCellBadgeNeue,可惜我们无法访问它。所以我认为唯一的方法是自定义tableview或tableviewCell而不是原来的一。
  • @ColeXia-MSFT 我尝试在WillShowViewController 中更改_UITableViewCellBadgeNeue 的颜色。但是,它只有在我从更多选项卡到优先级/按选项卡来回(至少需要 2-3 次)之后才有效。它永远不会第一次起作用。
  • 能否附上代码帮助我们重现?
  • @ColeXia-MSFT 我已经添加了代码

标签: c# xamarin.ios uitabbarcontroller uitabbaritem


【解决方案1】:

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

这是因为当你在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是哪一层。

【讨论】:

  • 如果我把代码放在DidShowViewController,它将以灰色和白色背景色显示徽章值,然后在几秒钟内,它会变为白色和清晰的背景色。
  • @currarpickt 是的,这是个问题,但它比推送到下一个 viewController 并返回要好...
  • 还没解决。即使我将代码放在DidShowViewController 中,有时它也不起作用(这是随机的,无法预测)。如果发生这种情况,我需要在控制器之间来回切换。
  • 那么人们正在做什么来解决这个问题?我有一个类似的问题,我正在尝试解决深色背景。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多