Skip to content

Commit 40d6640

Browse files
Shalini-Ashokankubaflo
authored andcommitted
[iOS/macCatalyst] Fix Entry and Editor BackgroundColor reset when set to null (#34741)
<!-- Please let the below note in for people that find this PR --> > [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details On iOS and macCatalyst, setting BackgroundColor = null on an Entry or Editor at runtime does not restore the native default appearance. Once a custom color is applied, it persists even after the property is cleared. ### Root Cause The shared iOS background update logic only resets BackgroundColor = null for LayoutView subclasses — all other UIView types silently return. MauiTextField (Entry) and MauiTextView (Editor) are not LayoutView subclasses, so the reset is skipped. ### Description of Change Platform-specific MapBackground methods were added to EntryHandler.iOS.cs and EditorHandler.iOS.cs. When Background.IsNullOrEmpty(), they now explicitly set platformView.BackgroundColor = null to restore native appearance. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #34611 ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/5ca30c6d-c069-4c04-989b-4dae36584cb4" >| <video src="https://github.com/user-attachments/assets/ee9e2a2e-c210-47cc-9f85-2526780d398b">| --------- Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
1 parent 1ec5e65 commit 40d6640

11 files changed

Lines changed: 146 additions & 2 deletions

File tree

51.7 KB
Loading
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 34611, "Entry and Editor BackgroundColor not reset to Null", PlatformAffected.iOS | PlatformAffected.macOS)]
4+
public class Issue34611 : TestContentPage
5+
{
6+
Entry _entry;
7+
Editor _editor;
8+
9+
protected override void Init()
10+
{
11+
Title = "Issue34611";
12+
13+
_entry = new Entry
14+
{
15+
AutomationId = "TestEntry",
16+
Text = "Entry background should reset",
17+
Placeholder = "Entry"
18+
};
19+
20+
_editor = new Editor
21+
{
22+
AutomationId = "TestEditor",
23+
Text = "Editor background should reset",
24+
HeightRequest = 120,
25+
Placeholder = "Editor"
26+
};
27+
28+
var applyButton = new Button
29+
{
30+
AutomationId = "ApplyBackgroundColorButton",
31+
Text = "Apply BackgroundColor"
32+
};
33+
34+
applyButton.Clicked += (_, _) =>
35+
{
36+
_entry.BackgroundColor = Colors.Red;
37+
_editor.BackgroundColor = Colors.LightBlue;
38+
};
39+
40+
var resetButton = new Button
41+
{
42+
AutomationId = "ResetToDefaultButton",
43+
Text = "Reset to Default"
44+
};
45+
46+
resetButton.Clicked += (_, _) =>
47+
{
48+
_entry.BackgroundColor = null;
49+
_editor.BackgroundColor = null;
50+
};
51+
52+
Content = new VerticalStackLayout
53+
{
54+
Margin = new Thickness(20, 0, 20, 0),
55+
Spacing = 12,
56+
Children =
57+
{
58+
new Label
59+
{
60+
Text = "Apply custom backgrounds, then reset them to null."
61+
},
62+
_entry,
63+
_editor,
64+
applyButton,
65+
resetButton
66+
}
67+
};
68+
}
69+
}
22.5 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue34611 : _IssuesUITest
8+
{
9+
public Issue34611(TestDevice device) : base(device)
10+
{
11+
}
12+
13+
public override string Issue => "Entry and Editor BackgroundColor not reset to Null";
14+
15+
[Test]
16+
[Category(UITestCategories.Entry)]
17+
public void EntryAndEditorBackgroundColorShouldResetToDefaultWhenSetToNull()
18+
{
19+
App.WaitForElement("ApplyBackgroundColorButton");
20+
App.Tap("ApplyBackgroundColorButton");
21+
App.WaitForElement("TestEntry");
22+
App.Tap("ResetToDefaultButton");
23+
VerifyScreenshot();
24+
}
25+
}
14.2 KB
Loading
51.7 KB
Loading
51.1 KB
Loading

src/Core/src/Handlers/Editor/EditorHandler.iOS.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ public static void MapText(IEditorHandler handler, IEditor editor)
8484
MapFormatting(handler, editor);
8585
}
8686

87+
public static void MapBackground(IEditorHandler handler, IEditor editor)
88+
{
89+
if (handler.PlatformView is not MauiTextView platformView)
90+
return;
91+
92+
if (editor.Background is ImageSourcePaint image)
93+
{
94+
var provider = handler.GetRequiredService<IImageSourceServiceProvider>();
95+
platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider)
96+
.FireAndForget(handler);
97+
}
98+
else if (editor.Background.IsNullOrEmpty())
99+
{
100+
platformView.RemoveBackgroundLayer();
101+
platformView.BackgroundColor = null;
102+
}
103+
else
104+
{
105+
platformView.UpdateBackground(editor);
106+
}
107+
}
108+
87109
public static void MapTextColor(IEditorHandler handler, IEditor editor) =>
88110
handler.PlatformView?.UpdateTextColor(editor);
89111

src/Core/src/Handlers/Entry/EntryHandler.iOS.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ public static void MapText(IEntryHandler handler, IEntry entry)
4848
MapFormatting(handler, entry);
4949
}
5050

51+
public static void MapBackground(IEntryHandler handler, IEntry entry)
52+
{
53+
if (handler.PlatformView is not MauiTextField platformView)
54+
return;
55+
56+
if (entry.Background is ImageSourcePaint image)
57+
{
58+
var provider = handler.GetRequiredService<IImageSourceServiceProvider>();
59+
platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider)
60+
.FireAndForget(handler);
61+
return;
62+
}
63+
else if (entry.Background.IsNullOrEmpty())
64+
{
65+
platformView.RemoveBackgroundLayer();
66+
platformView.BackgroundColor = null;
67+
return;
68+
}
69+
else
70+
{
71+
platformView.UpdateBackground(entry);
72+
}
73+
}
74+
5175
public static void MapTextColor(IEntryHandler handler, IEntry entry)
5276
{
5377
handler.PlatformView?.UpdateTextColor(entry);
@@ -212,8 +236,8 @@ void OnEditingChanged(object? sender, EventArgs e)
212236

213237
VirtualView.UpdateText(platformView.Text);
214238
}
215-
}
216-
239+
}
240+
217241
void OnEditingEnded(object? sender, EventArgs e)
218242
{
219243
if (sender is MauiTextField platformView && VirtualView is IEntry virtualView)

src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ override Microsoft.Maui.Handlers.StepperHandler.GetDesiredSize(double widthConst
55
override Microsoft.Maui.Platform.MauiTextView.TextAlignment.get -> UIKit.UITextAlignment
66
override Microsoft.Maui.Platform.MauiTextView.TextAlignment.set -> void
77
override Microsoft.Maui.Platform.MauiView.DidUpdateFocus(UIKit.UIFocusUpdateContext! context, UIKit.UIFocusAnimationCoordinator! coordinator) -> void
8+
static Microsoft.Maui.Handlers.EditorHandler.MapBackground(Microsoft.Maui.Handlers.IEditorHandler! handler, Microsoft.Maui.IEditor! editor) -> void
9+
static Microsoft.Maui.Handlers.EntryHandler.MapBackground(Microsoft.Maui.Handlers.IEntryHandler! handler, Microsoft.Maui.IEntry! entry) -> void
810
static Microsoft.Maui.Handlers.DatePickerHandler.MapFlowDirection(Microsoft.Maui.Handlers.IDatePickerHandler! handler, Microsoft.Maui.IDatePicker! datePicker) -> void

0 commit comments

Comments
 (0)