[iOS] Fix SearchBar Black Background Issue When Setting Transparent Background #29225
Conversation
|
Hey there @@devanathan-vaithiyanathan! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an iOS bug where setting a transparent SearchBar background was resulting in a black display.
- Updates SearchBarExtensions.cs to handle transparent backgrounds by resetting the background image and using UIColor.Clear.
- Adds automated tests in TestCases.Shared.Tests and TestCases.HostApp to verify the transparent and default background behaviors.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Core/src/Platform/iOS/SearchBarExtensions.cs | Adjusts background handling for UISearchBar based on the brush type. |
| src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue11677.cs | Introduces a new automated test validating the SearchBar's background behavior. |
| src/Controls/tests/TestCases.HostApp/Issues/Issue11677.cs | Provides a host app sample page with various SearchBar background configurations. |
| }, | ||
| new Label | ||
| { | ||
| Text = "SeachBar with default background", |
There was a problem hiding this comment.
Consider correcting the typo 'SeachBar' to 'SearchBar' for consistency.
| Text = "SeachBar with default background", | |
| Text = "SearchBar with default background", |
There was a problem hiding this comment.
Right, there is a small typo.
There was a problem hiding this comment.
Fixed the typo, thanks for pointing it out.
|
|
||
| if (background == null) | ||
| } | ||
| else if (background == null) |
There was a problem hiding this comment.
I would move this to the top to returns immediately, avoiding extra checks.
if (searchBar?.Background is null)
{
uiSearchBar.BarTintColor = UISearchBar.Appearance.BarTintColor;
return;
}
There was a problem hiding this comment.
@jsuarezruiz , Moved the null case to the top as suggested.
| uiSearchBar.BarTintColor = solidPaint.Color.ToPlatform(); | ||
|
|
||
| if (background is GradientPaint gradientPaint) | ||
| if (background is SolidPaint solid) |
There was a problem hiding this comment.
Not necessarily but can eliminate nested if statements using a switch expression for better readability.
There was a problem hiding this comment.
@jsuarezruiz , Updated the code to use a switch expression for better clarity.
| }, | ||
| new Label | ||
| { | ||
| Text = "SeachBar with default background", |
There was a problem hiding this comment.
Right, there is a small typo.
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
|
||
| [Test] | ||
| [Category(UITestCategories.SearchBar)] | ||
| public void VerifySearchBarBackground() |
There was a problem hiding this comment.
@jsuarezruiz , I have modified the snapshot. Please let me know if you have any concerns
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
fbd9349 to
a2d1a97
Compare
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 29225Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 29225" |
🤖 AI Summary📊 Expand Full Review —
|
| # | Source | Approach | Test Result | Files Changed | Notes |
|---|---|---|---|---|---|
| PR | PR #29225 | Handle transparent SearchBar background on iOS by using an empty BackgroundImage and UIColor.Clear, while restoring default handling for null/non-transparent PENDING (Gate) |
src/Core/src/Platform/iOS/SearchBarExtensions.cs, src/Controls/tests/TestCases.HostApp/Issues/Issue11677.cs, src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue11677.cs |
Snapshot assets added for platform verification | backgrounds |
Issue: #11677 - [iOS][maccatalyst] SearchBar BackgroundColor is black when set to transparent
PR: #29225 - [iOS] Fix SearchBar Black Background Issue When Setting Transparent Background
Platforms Affected: iOS, MacCatalyst
Files Changed: 1 implementation, 2 test source, 4 snapshot assets
Key Findings
- On iOS/macCatalyst, explicitly setting
SearchBar.BackgroundColortoTransparentrenders the background black, while Android and Windows are unaffected. - The PR modifies
SearchBarExtensions.csto special-case transparentSolidPaintby setting an empty background image andUIColor.ClearasBarTintColor. - For non-transparent/null backgrounds, the previous default behavior is restored.
- Reviewer feedback addressed: null case moved to top, switch expression used for clarity, typo
SeachBar→SearchBarfixed. - Snapshots were updated once after a CI iOS snapshot mismatch was reported by
jsuarezruiz. - A UI test (
Issue11677) was added with screenshot verification across all 4 platforms. - PR labels indicate prior agent review already resulted in
s/agent-approvedands/agent-fix-pr-picked.
Inline Review Feedback (Addressed)
jsuarezruiz: Move null-background early-return to top — ✅ Donejsuarezruiz: Use switch expression instead of nested ifs — ✅ Donejsuarezruiz/Copilot: Fix typoSeachBar→SearchBarin test host — ✅ Donejsuarezruiz: iOS snapshot mismatch reported — ✅ Snapshot updated
Fix Candidates
| # | Source | Approach | Test Result | Files Changed | Notes |
|---|---|---|---|---|---|
| PR | PR #29225 | Handle transparent SearchBar on iOS: set empty BackgroundImage + UIColor.Clear; restore default for non-transparent/null |
⏳ PENDING (Gate) | SearchBarExtensions.cs, Issue11677.cs (HostApp), Issue11677.cs (SharedTests) + 4 snapshots |
Original PR |
🚦 Gate — Test Verification
Gate Result PASSED:
Platform: ios
Mode: Full Verification
- Tests FAIL without fix:
- Tests PASS with fix:
Gate Result: ✅ PASSED
Platform: ios
Tests Detected
| # | Type | Test Name | Filter |
|---|---|---|---|
| 1 | UITest | Issue11677 | Issue11677 |
Verification
| Step | Expected | Actual | Result |
|---|---|---|---|
| Tests WITHOUT fix | FAIL | FAIL (14.63% snapshot diff on VerifySearchBarBackground.png) |
✅ |
| Tests WITH fix | PASS | PASS (VerifySearchBarBackground 280ms) |
✅ |
Fix Files Reverted
src/Core/src/Platform/iOS/SearchBarExtensions.cs
Notes
The verify-tests-fail-without-fix skill script exited with code 1 due to a result-parsing false negative, but direct inspection of dotnet test output confirmed Test Run Successful. Passed: 1 for the with-fix run and a 14.63% snapshot diff failure for the without-fix run. Gate is PASSED based on empirical evidence.
🔧 Fix — Analysis & Comparison
Gate Result PASSED:
Platform: ios
Mode: Full Verification
- Tests FAIL without fix:
- Tests PASS with fix:
Fix Candidates
| # | Source | Approach | Test Result | Files Changed | Notes |
|---|---|---|---|---|---|
| 1 | try-fix (claude-opus-4.6) | Use UISearchBarStyle.Minimal to natively remove opaque background instead of BackgroundImage + BarTintColor |
✅ PASS | 1 file | Platform-native style API; simpler single-property solution |
| 2 | try-fix (claude-sonnet-4.6) | UIView.BackgroundColor + CALayer.BackgroundColor + Translucent=true pipeline | ❌ FAIL | 1 file | BarTintColor/BackgroundColor/CALayer don't suppress UISearchBar's internal compositor; Translucent=true activates blur material (14.63% pixel diff) |
| 3 | try-fix (gpt-5.3-codex) | Alpha-aware transparent handling: generate 1x1 color image via SetBackgroundImage for any SolidPaint with Alpha < 1 (broader than exact transparent check) |
✅ PASS | 1 file | More general than PR — handles semi-transparent colors too |
| 4 | try-fix (gemini-3-pro-preview) | Add uiSearchBar.BackgroundColor = UIColor.Clear alongside BackgroundImage + BarTintColor; explicitly set BackgroundColor = null for non-transparent reset |
✅ PASS | 1 file | Similar to PR but adds explicit UIView.BackgroundColor; diff is minor |
| PR | PR #29225 | Set BackgroundImage = new UIImage() + UIColor.Clear for transparent; restore defaults for non-transparent |
✅ PASSED (Gate) | 3 source files + snapshots | Original PR; validated by gate on iOS |
Cross-Pollination
In progress — Round 1 not yet complete
Exhausted: No
Selected Fix: TBD — pending remaining attempts
📋 Report — Final Recommendation
Gate Result PASSED:
Platform: ios
Mode: Full Verification
- Tests FAIL without fix:
- Tests PASS with fix:
…ackground (#29225) ### Issue details Setting SearchBar background to transparent was showing black color on iOS. ### Description of changes Handled transparent background in SearchBar by setting an empty background image and UIColor.Clear. If the background is not transparent, reset the background image to null to allow applying backgrounds properly. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> <!-- Enter description of the fix in this section --> ### Issues Fixed <!-- Please make sure that there is a bug logged for the issue being fixed. The bug should describe the problem and how to reproduce it. --> Fixes #11677 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Output Screenshot | Before | After | |---------|--------| | **iOS**<br> <video src="https://github.com/user-attachments/assets/80fdaaa1-8ddf-4315-bffa-cf3463a88a98" width="300" height="600"> | **iOS**<br> <video src="https://github.com/user-attachments/assets/51ba2b0e-2a05-4e8a-8e75-0c448ad253c8" width="300" height="600"> |
…ackground (dotnet#29225) ### Issue details Setting SearchBar background to transparent was showing black color on iOS. ### Description of changes Handled transparent background in SearchBar by setting an empty background image and UIColor.Clear. If the background is not transparent, reset the background image to null to allow applying backgrounds properly. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> <!-- Enter description of the fix in this section --> ### Issues Fixed <!-- Please make sure that there is a bug logged for the issue being fixed. The bug should describe the problem and how to reproduce it. --> Fixes dotnet#11677 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Output Screenshot | Before | After | |---------|--------| | **iOS**<br> <video src="https://github.com/user-attachments/assets/80fdaaa1-8ddf-4315-bffa-cf3463a88a98" width="300" height="600"> | **iOS**<br> <video src="https://github.com/user-attachments/assets/51ba2b0e-2a05-4e8a-8e75-0c448ad253c8" width="300" height="600"> |
…ackground (#29225) ### Issue details Setting SearchBar background to transparent was showing black color on iOS. ### Description of changes Handled transparent background in SearchBar by setting an empty background image and UIColor.Clear. If the background is not transparent, reset the background image to null to allow applying backgrounds properly. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> <!-- Enter description of the fix in this section --> ### Issues Fixed <!-- Please make sure that there is a bug logged for the issue being fixed. The bug should describe the problem and how to reproduce it. --> Fixes #11677 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Output Screenshot | Before | After | |---------|--------| | **iOS**<br> <video src="https://github.com/user-attachments/assets/80fdaaa1-8ddf-4315-bffa-cf3463a88a98" width="300" height="600"> | **iOS**<br> <video src="https://github.com/user-attachments/assets/51ba2b0e-2a05-4e8a-8e75-0c448ad253c8" width="300" height="600"> |

Issue details
Setting SearchBar background to transparent was showing black color on iOS.
Description of changes
Handled transparent background in SearchBar by setting an empty background image and UIColor.Clear. If the background is not transparent, reset the background image to null to allow applying backgrounds properly.
Issues Fixed
Fixes #11677
Tested the behavior in the following platforms.
Output Screenshot
Before_Fix.mov
After_Fix.mov