【发布时间】: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 的层次结构后,显然Priority 和DueBy 选项卡的徽章值分配给_UITableViewCellBadgeNeue 内的UILabel。层次结构是:
-
this.MoreNavigationController.ViewControllers[0]:这是UIMoreListController- 获取
View并将其转换为UITableView,因为View是_UIMoreListTableView- 然后在该表视图
VisibleCells中进行迭代,检查IEnumerator,在第四个对象中有_UITableViewCellBadgeNeue-
_UITableViewCellBadgeNeue中的SubViews[0]是UILabel,标签的文本是徽章值。
-
- 然后在该表视图
- 获取
基于此,我在WillShowViewController 中更改标签TextColor 和BackgroundColor。它有效,但我需要从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