/
TabbedViewExtensions.cs
107 lines (90 loc) · 5.45 KB
/
TabbedViewExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Text;
using Foundation;
using Microsoft.Maui.Graphics;
using UIKit;
namespace Microsoft.Maui.Platform
{
internal static class TabbedViewExtensions
{
[System.Runtime.Versioning.SupportedOSPlatform("ios15.0")]
[System.Runtime.Versioning.SupportedOSPlatform("tvos15.0")]
internal static void UpdateiOS15TabBarAppearance(
this UITabBar tabBar,
ref UITabBarAppearance _tabBarAppearance,
UIColor? defaultBarColor,
UIColor? defaultBarTextColor,
Color? selectedTabColor,
Color? unselectedTabColor,
Color? barBackgroundColor,
Color? selectedBarTextColor,
Color? unSelectedBarTextColor)
{
if (_tabBarAppearance == null)
{
_tabBarAppearance = new UITabBarAppearance();
_tabBarAppearance.ConfigureWithDefaultBackground();
}
var effectiveBarColor = (barBackgroundColor == null) ? defaultBarColor : barBackgroundColor.ToPlatform();
// Set BarBackgroundColor
if (effectiveBarColor != null)
{
_tabBarAppearance.BackgroundColor = effectiveBarColor;
}
// Set BarTextColor
var effectiveSelectedBarTextColor = (selectedBarTextColor == null) ? defaultBarTextColor : selectedBarTextColor.ToPlatform();
var effectiveUnselectedBarTextColor = (unSelectedBarTextColor == null) ? defaultBarTextColor : unSelectedBarTextColor.ToPlatform();
// Update colors for all variations of the appearance to also make it work for iPads, etc. which use different layouts for the tabbar
// Also, set ParagraphStyle explicitly. This seems to be an iOS bug. If we don't do this, tab titles will be truncat...
// Set SelectedTabColor
if (selectedTabColor is not null)
{
var foregroundColor = selectedTabColor.ToPlatform();
var titleColor = effectiveSelectedBarTextColor ?? foregroundColor;
_tabBarAppearance.StackedLayoutAppearance.Selected.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.StackedLayoutAppearance.Selected.IconColor = foregroundColor;
_tabBarAppearance.InlineLayoutAppearance.Selected.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.InlineLayoutAppearance.Selected.IconColor = foregroundColor;
_tabBarAppearance.CompactInlineLayoutAppearance.Selected.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.CompactInlineLayoutAppearance.Selected.IconColor = foregroundColor;
}
else
{
var foregroundColor = UITabBar.Appearance.TintColor;
var titleColor = effectiveSelectedBarTextColor ?? foregroundColor;
_tabBarAppearance.StackedLayoutAppearance.Selected.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.StackedLayoutAppearance.Selected.IconColor = foregroundColor;
_tabBarAppearance.InlineLayoutAppearance.Selected.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.InlineLayoutAppearance.Selected.IconColor = foregroundColor;
_tabBarAppearance.CompactInlineLayoutAppearance.Selected.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.CompactInlineLayoutAppearance.Selected.IconColor = foregroundColor;
}
// Set UnselectedTabColor
if (unselectedTabColor is not null)
{
var foregroundColor = unselectedTabColor.ToPlatform();
var titleColor = effectiveUnselectedBarTextColor ?? foregroundColor;
_tabBarAppearance.StackedLayoutAppearance.Normal.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.StackedLayoutAppearance.Normal.IconColor = foregroundColor;
_tabBarAppearance.InlineLayoutAppearance.Normal.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.InlineLayoutAppearance.Normal.IconColor = foregroundColor;
_tabBarAppearance.CompactInlineLayoutAppearance.Normal.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.CompactInlineLayoutAppearance.Normal.IconColor = foregroundColor;
}
else
{
var foreground = UITabBar.Appearance.TintColor;
var titleColor = effectiveUnselectedBarTextColor ?? foreground;
_tabBarAppearance.StackedLayoutAppearance.Normal.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.StackedLayoutAppearance.Normal.IconColor = foreground;
_tabBarAppearance.InlineLayoutAppearance.Normal.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.InlineLayoutAppearance.Normal.IconColor = foreground;
_tabBarAppearance.CompactInlineLayoutAppearance.Normal.TitleTextAttributes = new UIStringAttributes { ForegroundColor = titleColor, ParagraphStyle = NSParagraphStyle.Default };
_tabBarAppearance.CompactInlineLayoutAppearance.Normal.IconColor = foreground;
}
// Set the TabBarAppearance
tabBar.StandardAppearance = tabBar.ScrollEdgeAppearance = _tabBarAppearance;
}
}
}