Skip to content

[.NET 10] [iOS] Fixed: Switch OffColor resets after minimizing and reopening the app #29771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Tamilarasan-Paranthaman
Copy link
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Root Cause of the issue

  • When the application is minimized and then reopened, the default style appears to be reapplied to the Switch, causing the custom OffColor to be lost. As a result, the Switch reverts to the default color instead of retaining the assigned OffColor.

Description of Change

  • Added an AddObserver for the app reopening notification on iOS and macOS. This observer ensures that the Switch OffColor is reapplied when the app is brought back to the foreground. The updated implementation restores the expected behavior.

Issues Fixed

Fixes #29768

Tested the behaviour in the following platforms

  • iOS
  • Mac
  • Android
  • Windows

Screenshot

Platform Before Fix After Fix
iOS
Before-Fix-iOS.mov
After-Fix-iOS.mov
Catalyst
Before-Fix-Mac.mov
After-Fix-Mac.mov

@dotnet-policy-service dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels Jun 2, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Tamilarasan-Paranthaman Tamilarasan-Paranthaman marked this pull request as ready for review June 2, 2025 14:49
@Copilot Copilot AI review requested due to automatic review settings June 2, 2025 14:49
@Tamilarasan-Paranthaman Tamilarasan-Paranthaman requested a review from a team as a code owner June 2, 2025 14:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue on iOS and Mac where the Switch OffColor resets when the app is minimized and reopened.

  • Added NSNotification observers to update the switch track color when the app returns to the foreground.
  • Updated the Disconnect method to remove the observers, preventing potential memory leaks.
  • Introduced test cases to validate the restoration of the OffColor after the app lifecycle events.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/Core/src/Handlers/Switch/SwitchHandler.iOS.cs Added observers to reapply OffColor on app foreground events and updated disconnect logic
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29768.cs Added test case verifying switch OffColor restoration after app reopening
src/Controls/tests/TestCases.HostApp/Issues/Issue29768.cs Added UI test page to support issue 29768 testing

Comment on lines +86 to +92
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
Copy link
Preview

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.

@rmarinho
Copy link
Member

rmarinho commented Jun 3, 2025

How does this relates to #29694 ?

@Tamilarasan-Paranthaman
Copy link
Contributor Author

How does this relates to #29694 ?

@rmarinho, this change is not directly related to #29694, as it addresses a different issue. Issue #29694 concerns the default On color not being retained, whereas this change fixes a problem where the Off color is not retained when the app is minimized and reopened

@rmarinho
Copy link
Member

rmarinho commented Jun 4, 2025

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen added this to the .NET 10.0-rc1 milestone Jun 10, 2025
@PureWeen PureWeen merged commit 841e43e into dotnet:net10.0 Jul 1, 2025
130 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-switch Switch community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants