Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29768.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29768, "Switch OffColor not displayed after minimizing and reopening the app", PlatformAffected.iOS)]
public class Issue29768 : ContentPage
{
public Issue29768()
{

var defaultOffSwitch = new Switch
{
OffColor = Colors.Red,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};

var switchControl = new Switch
{
IsToggled = true,
OffColor = Colors.Red,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};

var button = new Button
{
Text = "Toggle Switch 2",
AutomationId = "toggleButton",
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};

button.Clicked += (sender, e) =>
{
switchControl.IsToggled = !switchControl.IsToggled;
};

var verticalStackLayout = new VerticalStackLayout()
{
Spacing = 20,
Padding = new Thickness(20),
};

verticalStackLayout.Add(defaultOffSwitch);
verticalStackLayout.Add(switchControl);
verticalStackLayout.Add(button);

Content = verticalStackLayout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_CATALYST //The test fails on Windows and MacCatalyst because the BackgroundApp and ForegroundApp method, which is only supported on mobile platforms iOS and Android.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29768 : _IssuesUITest
{
public override string Issue => "Switch OffColor not displayed after minimizing and reopening the app";

public Issue29768(TestDevice device)
: base(device)
{ }

[Test]
[Category(UITestCategories.Switch)]
public void VerifySwitchOffColorAfterReopeningApp()
{
App.WaitForElement("toggleButton");
App.Tap("toggleButton");
App.BackgroundApp();
App.ForegroundApp();

VerifyScreenshot();
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions src/Core/src/Handlers/Switch/SwitchHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using Microsoft.Maui.Graphics;
using System.Threading.Tasks;
using CoreFoundation;
using Foundation;
using ObjCRuntime;
using UIKit;
using RectangleF = CoreGraphics.CGRect;
Expand Down Expand Up @@ -60,21 +63,81 @@ class SwitchProxy

ISwitch? VirtualView => _virtualView is not null && _virtualView.TryGetTarget(out var v) ? v : null;

WeakReference<UISwitch>? _platformView;

UISwitch? PlatformView => _platformView is not null && _platformView.TryGetTarget(out var p) ? p : null;

NSObject? _willEnterForegroundObserver;
NSObject? _windowDidBecomeKeyObserver;

public void Connect(ISwitch virtualView, UISwitch platformView)
{
_virtualView = new(virtualView);
_platformView = new(platformView);
platformView.ValueChanged += OnControlValueChanged;

#if MACCATALYST
_windowDidBecomeKeyObserver = NSNotificationCenter.DefaultCenter.AddObserver(
new NSString("NSWindowDidBecomeKeyNotification"), _ =>
{
if (PlatformView is not null)
{
UpdateTrackOffColor(PlatformView);
}
});
#elif IOS
_willEnterForegroundObserver = NSNotificationCenter.DefaultCenter.AddObserver(
UIApplication.WillEnterForegroundNotification, _ =>
{
if (PlatformView is not null)
{
UpdateTrackOffColor(PlatformView);
}
});
#endif
}

// Ensures the Switch track "OFF" color is updated correctly after system-level UI resets.
// This is necessary because UIKit may re-apply default styles to internal views during certain lifecycle events,
// especially when the app enters the background and returns to the foreground.
void UpdateTrackOffColor(UISwitch platformView)
{
DispatchQueue.MainQueue.DispatchAsync(async () =>
{
if (!platformView.On)
{
await Task.Delay(10); // Small delay, necessary to allow UIKit to complete its internal layout and styling processes before re-applying the custom color
Comment on lines +103 to +109
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the delay value '10' into a named constant to improve maintainability and clarify its purpose.

Suggested change
void UpdateTrackOffColor(UISwitch platformView)
{
DispatchQueue.MainQueue.DispatchAsync(async () =>
{
if (!platformView.On)
{
await Task.Delay(10); // Small delay, necessary to allow UIKit to complete its internal layout and styling processes before re-applying the custom color
const int UIKitStylingDelayMilliseconds = 10; // Small delay to allow UIKit to complete internal layout and styling processes
void UpdateTrackOffColor(UISwitch platformView)
{
DispatchQueue.MainQueue.DispatchAsync(async () =>
{
if (!platformView.On)
{
await Task.Delay(UIKitStylingDelayMilliseconds);

Copilot uses AI. Check for mistakes.

if (VirtualView is ISwitch view && view.TrackColor is not null)
{
platformView.UpdateTrackColor(view);
}
}
});
}

public void Disconnect(UISwitch platformView)
{
platformView.ValueChanged -= OnControlValueChanged;

if (_willEnterForegroundObserver is not null)
{
NSNotificationCenter.DefaultCenter.RemoveObserver(_willEnterForegroundObserver);
_willEnterForegroundObserver = null;
}
if (_windowDidBecomeKeyObserver is not null)
{
NSNotificationCenter.DefaultCenter.RemoveObserver(_windowDidBecomeKeyObserver);
_windowDidBecomeKeyObserver = null;
}
}

void OnControlValueChanged(object? sender, EventArgs e)
{
if (VirtualView is ISwitch virtualView && sender is UISwitch platformView && virtualView.IsOn != platformView.On)
{
virtualView.IsOn = platformView.On;
}
}
}
}
Expand Down
Loading