我使用以下代码自定义背景颜色及其色调:
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
的层次结构后,显然Priority
和DueBy
标签的徽章价值已分配给UILabel
内的_UITableViewCellBadgeNeue
。层次结构是:
this.MoreNavigationController.ViewControllers[0]
:这是UIMoreListController
View
并将其投放到UITableView
,因为View
是_UIMoreListTableView
VisibleCells
内迭代,检查IEnumerator
,在第四个对象中_UITableViewCellBadgeNeue
SubViews[0]
内的_UITableViewCellBadgeNeue
为UILabel
,标签的文字为徽章价值。在此基础上,我更改了TextColor
中的标签BackgroundColor
和WillShowViewController
。它可以工作,但我需要在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++;
}
}
}
}
答案 0 :(得分:0)
我复制了你的问题并找出了原因。
这是因为当您在WillShowViewController
中检索视图层次结构时,TableView尚未完成渲染(绘图)。
在WillShowViewController
UITableViewCellContentView
UIButton
在DidShowViewController
UITableViewCellContentView
UIButton
_UITableViewCellBadgeNeue
_UITableViewCellSeparatorView
UITableViewCellContentView
UIButton
_UITableViewCellSeparatorView
因此,您只需将WillShowViewController
替换为DidShowViewController
。
为避免Apple更改View Hierarchy,您可以使用if (view.Class.Name.ToString() == "_UITableViewCellBadgeNeue")
之类的条件,而不是判断_UITableViewCellBadgeNeue
的级别。