gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:55:25 27 4
gpt4 key购买 nike

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

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
        • _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++;
}
}
}
}

最佳答案

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

这是因为当您在WillShowViewController 中检索 View 层次结构时,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 是。

关于c# - iOS 在 UITabBarController 中更改 UIMoreNavigationController 内的徽章颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com