Skip to content

Commit 29911a8

Browse files
praveenkumarkarunanithisheiksyedm
authored andcommitted
fix moved to common file.
1 parent 62eb7f5 commit 29911a8

File tree

3 files changed

+67
-115
lines changed

3 files changed

+67
-115
lines changed

src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.Maui.Controls.Internals;
77
using Microsoft.Maui.Controls.Platform;
88
using Microsoft.Maui.Graphics;
9-
using Microsoft.Maui.Platform;
109
using UIKit;
1110

1211
namespace Microsoft.Maui.Controls.Handlers.Items
@@ -141,67 +140,12 @@ public override void LayoutSubviews()
141140
// Inject per-cell safe area insets into the MauiView for CrossPlatformArrange
142141
// to apply as internal padding. UICollectionView bypasses MAUI's arrange chain,
143142
// so cells cannot use the standard safe area flow (#33604, #34635).
144-
if (virtualView is ISafeAreaView2 safeView && PlatformHandler.ToPlatform() is MauiView mauiView)
145-
{
146-
var insets = ComputeCellSafeAreaInsets(safeView);
147-
mauiView.CellSafeAreaOverride = insets != UIEdgeInsets.Zero
148-
? insets.ToSafeAreaInsets()
149-
: SafeAreaPadding.Empty;
150-
}
151-
else if (PlatformHandler.ToPlatform() is MauiView mv && !mv.CellSafeAreaOverride.IsEmpty)
152-
{
153-
// Clear stale override from a previous template that implemented ISafeAreaView2.
154-
mv.CellSafeAreaOverride = SafeAreaPadding.Empty;
155-
}
143+
MauiView.ApplyCellSafeAreaOverride(this, virtualView, PlatformHandler.ToPlatform());
156144

157145
virtualView.Arrange(frame);
158146
}
159147
}
160148

161-
/// <summary>
162-
/// Computes per-cell safe area insets based on geometric overlap with the window's unsafe regions.
163-
/// Returns <see cref="UIEdgeInsets.Zero"/> when all edges share the same region (e.g., default
164-
/// Container×4), as the parent layout chain handles uniform safe area (#33604, #34635).
165-
/// </summary>
166-
UIEdgeInsets ComputeCellSafeAreaInsets(ISafeAreaView2 safeView)
167-
{
168-
var window = Window;
169-
if (window is null)
170-
return UIEdgeInsets.Zero;
171-
172-
var windowSA = window.SafeAreaInsets;
173-
if (windowSA == UIEdgeInsets.Zero)
174-
return UIEdgeInsets.Zero;
175-
176-
var leftRegion = safeView.GetSafeAreaRegionsForEdge(0);
177-
var topRegion = safeView.GetSafeAreaRegionsForEdge(1);
178-
var rightRegion = safeView.GetSafeAreaRegionsForEdge(2);
179-
var bottomRegion = safeView.GetSafeAreaRegionsForEdge(3);
180-
181-
// Uniform edges (Container×4, None×4, All×4) are handled by the parent layout chain.
182-
bool allSameRegion = leftRegion == topRegion
183-
&& topRegion == rightRegion
184-
&& rightRegion == bottomRegion;
185-
186-
if (allSameRegion)
187-
return UIEdgeInsets.Zero;
188-
189-
// Only apply insets for Container edges; SoftInput-only edges are excluded.
190-
var cellInWindow = ConvertRectToView(Bounds, window);
191-
var windowBounds = window.Bounds;
192-
193-
nfloat left = SafeAreaEdges.IsContainer(leftRegion) && windowSA.Left > 0
194-
? (nfloat)Math.Max(0, (double)(windowSA.Left - cellInWindow.X)) : 0;
195-
nfloat top = SafeAreaEdges.IsContainer(topRegion) && windowSA.Top > 0
196-
? (nfloat)Math.Max(0, (double)(windowSA.Top - cellInWindow.Y)) : 0;
197-
nfloat right = SafeAreaEdges.IsContainer(rightRegion) && windowSA.Right > 0
198-
? (nfloat)Math.Max(0, (double)(cellInWindow.Right - (windowBounds.Width - windowSA.Right))) : 0;
199-
nfloat bottom = SafeAreaEdges.IsContainer(bottomRegion) && windowSA.Bottom > 0
200-
? (nfloat)Math.Max(0, (double)(cellInWindow.Bottom - (windowBounds.Height - windowSA.Bottom))) : 0;
201-
202-
return new UIEdgeInsets(top, left, bottom, right);
203-
}
204-
205149
[Obsolete]
206150
[EditorBrowsable(EditorBrowsableState.Never)]
207151
protected void Layout(CGSize constraints)

src/Controls/src/Core/Handlers/Items2/iOS/TemplatedCell2.cs

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.Maui.Controls.Internals;
77
using Microsoft.Maui.Controls.Platform;
88
using Microsoft.Maui.Graphics;
9-
using Microsoft.Maui.Platform;
109
using UIKit;
1110

1211
namespace Microsoft.Maui.Controls.Handlers.Items2
@@ -202,67 +201,12 @@ public override void LayoutSubviews()
202201
// Inject per-cell safe area insets into the MauiView for CrossPlatformArrange
203202
// to apply as internal padding. UICollectionView bypasses MAUI's arrange chain,
204203
// so cells cannot use the standard safe area flow (#33604, #34635).
205-
if (virtualView is ISafeAreaView2 safeView && PlatformView is MauiView mauiView)
206-
{
207-
var insets = ComputeCellSafeAreaInsets(safeView);
208-
mauiView.CellSafeAreaOverride = insets != UIEdgeInsets.Zero
209-
? insets.ToSafeAreaInsets()
210-
: SafeAreaPadding.Empty;
211-
}
212-
else if (PlatformView is MauiView mv && !mv.CellSafeAreaOverride.IsEmpty)
213-
{
214-
// Clear stale override from a previous template that implemented ISafeAreaView2.
215-
mv.CellSafeAreaOverride = SafeAreaPadding.Empty;
216-
}
204+
MauiView.ApplyCellSafeAreaOverride(this, virtualView, PlatformView);
217205

218206
virtualView.Arrange(frame);
219207
}
220208
}
221209

222-
/// <summary>
223-
/// Computes per-cell safe area insets based on geometric overlap with the window's unsafe regions.
224-
/// Returns <see cref="UIEdgeInsets.Zero"/> when all edges share the same region (e.g., default
225-
/// Container×4), as the parent layout chain handles uniform safe area (#33604, #34635).
226-
/// </summary>
227-
UIEdgeInsets ComputeCellSafeAreaInsets(ISafeAreaView2 safeView)
228-
{
229-
var window = Window;
230-
if (window is null)
231-
return UIEdgeInsets.Zero;
232-
233-
var windowSA = window.SafeAreaInsets;
234-
if (windowSA == UIEdgeInsets.Zero)
235-
return UIEdgeInsets.Zero;
236-
237-
var leftRegion = safeView.GetSafeAreaRegionsForEdge(0);
238-
var topRegion = safeView.GetSafeAreaRegionsForEdge(1);
239-
var rightRegion = safeView.GetSafeAreaRegionsForEdge(2);
240-
var bottomRegion = safeView.GetSafeAreaRegionsForEdge(3);
241-
242-
// Uniform edges (Container×4, None×4, All×4) are handled by the parent layout chain.
243-
bool allSameRegion = leftRegion == topRegion
244-
&& topRegion == rightRegion
245-
&& rightRegion == bottomRegion;
246-
247-
if (allSameRegion)
248-
return UIEdgeInsets.Zero;
249-
250-
// Only apply insets for Container edges; SoftInput-only edges are excluded.
251-
var cellInWindow = ConvertRectToView(Bounds, window);
252-
var windowBounds = window.Bounds;
253-
254-
nfloat left = SafeAreaEdges.IsContainer(leftRegion) && windowSA.Left > 0
255-
? (nfloat)Math.Max(0, (double)(windowSA.Left - cellInWindow.X)) : 0;
256-
nfloat top = SafeAreaEdges.IsContainer(topRegion) && windowSA.Top > 0
257-
? (nfloat)Math.Max(0, (double)(windowSA.Top - cellInWindow.Y)) : 0;
258-
nfloat right = SafeAreaEdges.IsContainer(rightRegion) && windowSA.Right > 0
259-
? (nfloat)Math.Max(0, (double)(cellInWindow.Right - (windowBounds.Width - windowSA.Right))) : 0;
260-
nfloat bottom = SafeAreaEdges.IsContainer(bottomRegion) && windowSA.Bottom > 0
261-
? (nfloat)Math.Max(0, (double)(cellInWindow.Bottom - (windowBounds.Height - windowSA.Bottom))) : 0;
262-
263-
return new UIEdgeInsets(top, left, bottom, right);
264-
}
265-
266210
public override void PrepareForReuse()
267211
{
268212
//Unbind();

src/Core/src/Platform/iOS/MauiView.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,77 @@ public abstract class MauiView : UIView, ICrossPlatformLayoutBacking, IVisualTre
6363
bool _appliesSafeAreaAdjustments;
6464

6565
/// <summary>
66-
/// Safe area override injected by TemplatedCell2 for CollectionView cells.
66+
/// Safe area override injected by CollectionView cells.
6767
/// UICollectionView bypasses MAUI's arrange chain, so cells cannot use <see cref="_safeArea"/>.
6868
/// Applied as internal padding by <see cref="CrossPlatformArrange"/> and
6969
/// <see cref="CrossPlatformMeasure"/> (#33604, #34635).
7070
/// </summary>
7171
internal SafeAreaPadding CellSafeAreaOverride { get; set; } = SafeAreaPadding.Empty;
7272

73+
/// <summary>
74+
/// Computes and applies per-cell safe area insets for a CollectionView cell.
75+
/// Called from TemplatedCell/TemplatedCell2 LayoutSubviews before Arrange.
76+
/// </summary>
77+
internal static void ApplyCellSafeAreaOverride(UIView cell, IView virtualView, UIView platformView)
78+
{
79+
if (virtualView is ISafeAreaView2 safeView && platformView is MauiView mauiView)
80+
{
81+
var insets = ComputeCellSafeAreaInsets(cell, safeView);
82+
mauiView.CellSafeAreaOverride = insets != UIEdgeInsets.Zero
83+
? insets.ToSafeAreaInsets()
84+
: SafeAreaPadding.Empty;
85+
}
86+
else if (platformView is MauiView mv && !mv.CellSafeAreaOverride.IsEmpty)
87+
{
88+
// Clear stale override from a previous template that implemented ISafeAreaView2.
89+
mv.CellSafeAreaOverride = SafeAreaPadding.Empty;
90+
}
91+
}
92+
93+
/// <summary>
94+
/// Computes per-cell safe area insets based on geometric overlap with the window's unsafe regions.
95+
/// Returns <see cref="UIEdgeInsets.Zero"/> when all edges share the same region (e.g., default
96+
/// Container×4), as the parent layout chain handles uniform safe area (#33604, #34635).
97+
/// </summary>
98+
static UIEdgeInsets ComputeCellSafeAreaInsets(UIView cell, ISafeAreaView2 safeView)
99+
{
100+
var window = cell.Window;
101+
if (window is null)
102+
return UIEdgeInsets.Zero;
103+
104+
var windowSA = window.SafeAreaInsets;
105+
if (windowSA == UIEdgeInsets.Zero)
106+
return UIEdgeInsets.Zero;
107+
108+
var leftRegion = safeView.GetSafeAreaRegionsForEdge(0);
109+
var topRegion = safeView.GetSafeAreaRegionsForEdge(1);
110+
var rightRegion = safeView.GetSafeAreaRegionsForEdge(2);
111+
var bottomRegion = safeView.GetSafeAreaRegionsForEdge(3);
112+
113+
// Uniform edges (Container×4, None×4, All×4) are handled by the parent layout chain.
114+
bool allSameRegion = leftRegion == topRegion
115+
&& topRegion == rightRegion
116+
&& rightRegion == bottomRegion;
117+
118+
if (allSameRegion)
119+
return UIEdgeInsets.Zero;
120+
121+
// Only apply insets for Container edges; SoftInput-only edges are excluded.
122+
var cellInWindow = cell.ConvertRectToView(cell.Bounds, window);
123+
var windowBounds = window.Bounds;
124+
125+
nfloat left = SafeAreaEdges.IsContainer(leftRegion) && windowSA.Left > 0
126+
? (nfloat)Math.Max(0, (double)(windowSA.Left - cellInWindow.X)) : 0;
127+
nfloat top = SafeAreaEdges.IsContainer(topRegion) && windowSA.Top > 0
128+
? (nfloat)Math.Max(0, (double)(windowSA.Top - cellInWindow.Y)) : 0;
129+
nfloat right = SafeAreaEdges.IsContainer(rightRegion) && windowSA.Right > 0
130+
? (nfloat)Math.Max(0, (double)(cellInWindow.Right - (windowBounds.Width - windowSA.Right))) : 0;
131+
nfloat bottom = SafeAreaEdges.IsContainer(bottomRegion) && windowSA.Bottom > 0
132+
? (nfloat)Math.Max(0, (double)(cellInWindow.Bottom - (windowBounds.Height - windowSA.Bottom))) : 0;
133+
134+
return new UIEdgeInsets(top, left, bottom, right);
135+
}
136+
73137
// Indicates whether this view should respond to safe area insets.
74138
// Cached to avoid repeated hierarchy checks.
75139
// True if the view is an ISafeAreaView, does not ignore safe area, and is not inside a UIScrollView;

0 commit comments

Comments
 (0)