You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS:如何修改UITabBarController中UIMoreNavigationController内的徽章颜色

修改UIMoreNavigationController中的徽章颜色

嘿,我来帮你搞定这个徽章颜色的问题!你已经成功自定义了More页面的背景色和文本色调,接下来只要针对徽章做些针对性调整就可以了。

方法一:直接遍历Cell子视图修改(快速实现)

系统的More页面单元格里的徽章,本质是嵌套在Cell子视图里的UILabel(不同iOS版本可能有细微结构差异)。你可以在现有的遍历VisibleCells的循环里,加上查找并修改徽章的逻辑:

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;
    
    // 查找并修改徽章颜色
    foreach (var subview in cell.Subviews)
    {
        // 系统默认徽章用的是圆点字符,以此判断
        if (subview is UILabel badgeLabel && !string.IsNullOrEmpty(badgeLabel.Text) && badgeLabel.Text[0] == '\u25CF')
        {
            badgeLabel.TextColor = UIColor.Red; // 设置你想要的徽章文本颜色
            badgeLabel.BackgroundColor = UIColor.Yellow; // 可选:修改徽章背景色
            break;
        }
        // 兜底:通过视图类名判断(适配不同iOS版本)
        else if (subview.Class.Name.Contains("Badge"))
        {
            if (subview.Subviews.FirstOrDefault() is UILabel badge)
            {
                badge.TextColor = UIColor.White;
                badge.BackgroundColor = UIColor.Green;
                badge.Layer.CornerRadius = badge.Frame.Height / 2; // 让徽章保持圆形
                badge.ClipsToBounds = true;
            }
            break;
        }
    }
}

方法二:自定义Cell(更稳定,适配多版本)

如果担心系统视图结构在不同iOS版本变动导致失效,自定义Cell是更稳妥的方案。你可以替换More页面的TableViewCell:

// 先注册自定义Cell
var moreTableView = (UITableView)this.MoreNavigationController.TopViewController.View;
moreTableView.RegisterClassForCellReuse(typeof(CustomMoreCell), UITableViewCell.DefaultReuseIdentifier);

// 自定义Cell类
public class CustomMoreCell : UITableViewCell
{
    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        
        // 调整徽章样式
        if (AccessoryView is UIView badgeContainer)
        {
            foreach (var sub in badgeContainer.Subviews)
            {
                if (sub is UILabel badge)
                {
                    badge.TextColor = UIColor.White;
                    badge.BackgroundColor = UIColor.Orange;
                    badge.Layer.CornerRadius = badge.Bounds.Height / 2;
                    badge.ClipsToBounds = true;
                }
            }
        }
    }
}

重要提醒!

只修改VisibleCells是不够的——当用户滚动页面时,新加载的Cell会恢复默认样式。所以你还需要在TableView的数据源方法里同步修改:

moreTableView.Source = new UITableViewSource()
{
    GetCell = (tableView, indexPath) =>
    {
        var cell = tableView.DequeueReusableCell(UITableViewCell.DefaultReuseIdentifier, indexPath);
        // 复用你已有的样式设置
        cell.BackgroundColor = blackBGColor;
        cell.TextLabel.TextColor = UIColor.White;
        
        // 这里同样加上修改徽章的逻辑,和上面遍历VisibleCells里的代码一致
        foreach (var subview in cell.Subviews)
        {
            if (subview is UILabel badgeLabel && !string.IsNullOrEmpty(badgeLabel.Text) && badgeLabel.Text[0] == '\u25CF')
            {
                badgeLabel.TextColor = UIColor.Red;
                badgeLabel.BackgroundColor = UIColor.Yellow;
                break;
            }
        }
        
        return cell;
    },
    // 别忘了实现其他必要的数据源方法,比如RowsInSection
    RowsInSection = (tableView, section) => this.ViewControllers.Length - 4 // More页面默认显示超过4个的Tab
};

这样就能确保所有More页面里的徽章都能保持你想要的样式啦!

内容的提问来源于stack exchange,提问作者currarpickt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:57:19