Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit e72762f

Browse files
samhoutsrmarinho
authored andcommitted
[iOS] Default BarTextColor/BarBackgroundColor will no longer override values set in custom renderers (#233)
* [Controls] Improve TabbedPage test case iOS should default to the color used in a custom renderer instead of to the global default color. * [iOS] TabbedPage Bar*Color default is better Will use the color set by a custom renderer, if any, instead of always pulling from global appearance.
1 parent 6dd32da commit e72762f

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

Xamarin.Forms.ControlGallery.iOS/CustomRenderers.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
[assembly: ExportRenderer(typeof(NativeListView2), typeof(NativeiOSListViewRenderer))]
2929
[assembly: ExportRenderer(typeof(NativeListView), typeof(NativeListViewRenderer))]
3030
[assembly: ExportRenderer(typeof(CustomMapView), typeof(CustomIOSMapRenderer))]
31-
31+
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageWithCustomBarColorRenderer))]
3232
namespace Xamarin.Forms.ControlGallery.iOS
3333
{
3434
public class CustomIOSMapRenderer : ViewRenderer<CustomMapView, MKMapView>
@@ -593,5 +593,16 @@ public CollectionViewCell(RectangleF frame) : base(frame)
593593
}
594594
}
595595

596+
public class TabbedPageWithCustomBarColorRenderer : TabbedRenderer
597+
{
598+
public TabbedPageWithCustomBarColorRenderer()
599+
{
600+
TabBar.TintColor = UIColor.White;
601+
TabBar.BarTintColor = UIColor.Purple;
602+
603+
//UITabBar.Appearance.TintColor = UIColor.White;
604+
//UITabBar.Appearance.BarTintColor = UIColor.Purple;
605+
}
606+
}
596607
}
597608

Xamarin.Forms.Controls/CoreGallery.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ internal class CoreNavigationPage : NavigationPage
6969
public CoreNavigationPage ()
7070
{
7171
AutomationId = "NavigationPageRoot";
72+
73+
BarBackgroundColor = Color.Maroon;
74+
BarTextColor = Color.Yellow;
75+
76+
Device.StartTimer(TimeSpan.FromSeconds(2), () => {
77+
BarBackgroundColor = Color.Default;
78+
BarTextColor = Color.Default;
79+
80+
return false;
81+
});
82+
7283
Navigation.PushAsync (new CoreRootPage (this));
7384
}
7485
}
@@ -85,8 +96,20 @@ public CoreTabbedPage ()
8596
{
8697
AutomationId = "TabbedPageRoot";
8798

88-
BarBackgroundColor = Color.Maroon;
89-
BarTextColor = Color.White;
99+
100+
Device.StartTimer(TimeSpan.FromSeconds(6), () => {
101+
BarBackgroundColor = Color.Maroon;
102+
BarTextColor = Color.Yellow;
103+
104+
Device.StartTimer(TimeSpan.FromSeconds(6), () => {
105+
BarBackgroundColor = Color.Default;
106+
BarTextColor = Color.Default;
107+
108+
return false;
109+
});
110+
111+
return false;
112+
});
90113

91114
Children.Add(new CoreRootPage(this, NavigationBehavior.PushModalAsync) { Title = "Tab 1" });
92115
Children.Add(new CoreRootPage(this, NavigationBehavior.PushModalAsync) { Title = "Tab 2" });

Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ namespace Xamarin.Forms.Platform.iOS
2323
{
2424
public class TabbedRenderer : UITabBarController, IVisualElementRenderer, IEffectControlProvider
2525
{
26+
bool _barBackgroundColorWasSet;
27+
bool _barTextColorWasSet;
28+
UIColor _defaultBarTextColor;
29+
bool _defaultBarTextColorSet;
30+
UIColor _defaultBarColor;
31+
bool _defaultBarColorSet;
2632
bool _loaded;
2733
Size _queuedSize;
2834

@@ -305,14 +311,31 @@ void UpdateBarBackgroundColor()
305311
return;
306312

307313
var barBackgroundColor = Tabbed.BarBackgroundColor;
314+
var isDefaultColor = barBackgroundColor.IsDefault;
315+
316+
if (isDefaultColor && !_barBackgroundColorWasSet)
317+
return;
318+
319+
if (!_defaultBarColorSet)
320+
{
321+
if (Forms.IsiOS7OrNewer)
322+
_defaultBarColor = TabBar.BarTintColor;
323+
else
324+
_defaultBarColor = TabBar.TintColor;
325+
326+
_defaultBarColorSet = true;
327+
}
328+
329+
if (!isDefaultColor)
330+
_barBackgroundColorWasSet = true;
308331

309332
if (Forms.IsiOS7OrNewer)
310333
{
311-
TabBar.BarTintColor = barBackgroundColor == Color.Default ? UINavigationBar.Appearance.BarTintColor : barBackgroundColor.ToUIColor();
334+
TabBar.BarTintColor = isDefaultColor ? _defaultBarColor : barBackgroundColor.ToUIColor();
312335
}
313336
else
314337
{
315-
TabBar.TintColor = barBackgroundColor == Color.Default ? UINavigationBar.Appearance.TintColor : barBackgroundColor.ToUIColor();
338+
TabBar.TintColor = isDefaultColor ? _defaultBarColor : barBackgroundColor.ToUIColor();
316339
}
317340
}
318341

@@ -322,13 +345,24 @@ void UpdateBarTextColor()
322345
return;
323346

324347
var barTextColor = Tabbed.BarTextColor;
348+
var isDefaultColor = barTextColor.IsDefault;
349+
350+
if (isDefaultColor && !_barTextColorWasSet)
351+
return;
352+
353+
if (!_defaultBarTextColorSet)
354+
{
355+
_defaultBarTextColor = TabBar.TintColor;
356+
_defaultBarTextColorSet = true;
357+
}
325358

326-
var globalAttributes = UINavigationBar.Appearance.GetTitleTextAttributes();
359+
if (!isDefaultColor)
360+
_barTextColorWasSet = true;
327361

328-
var attributes = new UITextAttributes { Font = globalAttributes.Font };
362+
var attributes = new UITextAttributes();
329363

330-
if (barTextColor == Color.Default)
331-
attributes.TextColor = globalAttributes.TextColor;
364+
if (isDefaultColor)
365+
attributes.TextColor = _defaultBarTextColor;
332366
else
333367
attributes.TextColor = barTextColor.ToUIColor();
334368

@@ -341,7 +375,7 @@ void UpdateBarTextColor()
341375
// setting the unselected icon tint is not supported by iOS
342376
if (Forms.IsiOS7OrNewer)
343377
{
344-
TabBar.TintColor = barTextColor == Color.Default ? UINavigationBar.Appearance.TintColor : barTextColor.ToUIColor();
378+
TabBar.TintColor = isDefaultColor ? _defaultBarTextColor : barTextColor.ToUIColor();
345379
}
346380
}
347381

0 commit comments

Comments
 (0)