Skip to content

Commit 1ebb238

Browse files
authored
Revert "Ensure that viewport calculations for iOS ScrollView account for content insets (#18277)" (#18579)
This reverts commit 0cb5d80.
1 parent 2fb3782 commit 1ebb238

File tree

3 files changed

+25
-73
lines changed

3 files changed

+25
-73
lines changed

src/Controls/tests/DeviceTests/Elements/ScrollView/ScrollViewTests.iOS.cs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.Maui.Graphics;
88
using Microsoft.Maui.Handlers;
99
using Microsoft.Maui.Hosting;
10-
using UIKit;
1110
using Xunit;
1211

1312
namespace Microsoft.Maui.DeviceTests
@@ -36,52 +35,5 @@ await scrollViewHandler.PlatformView.AttachAndRun(() =>
3635
});
3736
});
3837
}
39-
40-
[Fact]
41-
public async Task ContentSizeExpandsToViewport()
42-
{
43-
EnsureHandlerCreated(builder => { builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler<Entry, EntryHandler>(); }); });
44-
45-
var scrollView = new ScrollView();
46-
47-
var entry = new Entry() { Text = "In a ScrollView", HeightRequest = 10 };
48-
49-
50-
static CoreGraphics.CGSize getViewportSize(UIScrollView scrollView)
51-
{
52-
return scrollView.AdjustedContentInset.InsetRect(scrollView.Bounds).Size;
53-
};
54-
55-
var scrollViewHandler = await InvokeOnMainThreadAsync(() =>
56-
{
57-
return CreateHandlerAsync<ScrollViewHandler>(scrollView);
58-
});
59-
60-
await InvokeOnMainThreadAsync(async () =>
61-
{
62-
await scrollViewHandler.PlatformView.AttachAndRun(async () =>
63-
{
64-
var uiScrollView = scrollViewHandler.PlatformView;
65-
66-
uiScrollView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Always;
67-
var parent = uiScrollView.Superview;
68-
uiScrollView.Bounds = parent.Bounds;
69-
uiScrollView.Center = parent.Center;
70-
71-
scrollView.Content = entry;
72-
73-
parent.SetNeedsLayout();
74-
parent.LayoutIfNeeded();
75-
76-
await Task.Yield();
77-
78-
var contentSize = uiScrollView.ContentSize;
79-
var viewportSize = getViewportSize(uiScrollView);
80-
81-
Assert.Equal(viewportSize.Height, contentSize.Height);
82-
Assert.Equal(viewportSize.Width, contentSize.Width);
83-
});
84-
});
85-
}
8638
}
8739
}
-77 Bytes
Loading

src/Core/src/Handlers/ScrollView/ScrollViewHandler.iOS.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Drawing;
5+
using System.Text;
26
using CoreGraphics;
37
using Microsoft.Maui.Graphics;
48
using Microsoft.Maui.Layouts;
9+
using Microsoft.Maui.Platform;
10+
using ObjCRuntime;
511
using UIKit;
612
using Size = Microsoft.Maui.Graphics.Size;
713

@@ -97,11 +103,9 @@ public static void MapOrientation(IScrollViewHandler handler, IScrollView scroll
97103
// without having to re-layout the ScrollView
98104

99105
var fullContentSize = scrollView.PresentedContent?.DesiredSize ?? Size.Zero;
100-
101-
var viewportBounds = GetViewportBounds(uiScrollView);
106+
var viewportBounds = uiScrollView.Bounds;
102107
var viewportWidth = viewportBounds.Width;
103108
var viewportHeight = viewportBounds.Height;
104-
105109
SetContentSizeForOrientation(uiScrollView, viewportWidth, viewportHeight, scrollView.Orientation, fullContentSize);
106110
}
107111

@@ -123,7 +127,7 @@ public static void MapRequestScrollTo(IScrollViewHandler handler, IScrollView sc
123127
var availableScrollWidth = uiScrollView.ContentSize.Width - uiScrollView.Frame.Width;
124128
var minScrollHorizontal = Math.Min(request.HorizontalOffset, availableScrollWidth);
125129
var minScrollVertical = Math.Min(request.VerticalOffset, availableScrollHeight);
126-
uiScrollView.SetContentOffset(new CGPoint(minScrollHorizontal, minScrollVertical), !request.Instant);
130+
uiScrollView.SetContentOffset(new CoreGraphics.CGPoint(minScrollHorizontal, minScrollVertical), !request.Instant);
127131

128132
if (request.Instant)
129133
{
@@ -181,15 +185,16 @@ static void InsertContentView(UIScrollView platformScrollView, IScrollView scrol
181185
return;
182186
}
183187

184-
var contentContainer = new ContentView
188+
var contentContainer = new ContentView()
185189
{
186190
View = scrollView.PresentedContent,
187-
Tag = ContentPanelTag,
188-
// This is where we normally would inject the cross-platform ScrollView's layout logic; instead, we're injecting the
189-
// methods from this handler so it can make some adjustments for things like Padding before the default logic is invoked
190-
CrossPlatformLayout = crossPlatformLayout
191+
Tag = ContentPanelTag
191192
};
192193

194+
// This is where we normally would inject the cross-platform ScrollView's layout logic; instead, we're injecting the
195+
// methods from this handler so it can make some adjustments for things like Padding before the default logic is invoked
196+
contentContainer.CrossPlatformLayout = crossPlatformLayout;
197+
193198
platformScrollView.ClearSubviews();
194199
contentContainer.AddSubview(platformContent);
195200
platformScrollView.AddSubview(contentContainer);
@@ -284,11 +289,6 @@ static void SetContentSizeForOrientation(UIScrollView uiScrollView, double viewp
284289
uiScrollView.ContentSize = contentSize;
285290
}
286291

287-
static CGRect GetViewportBounds(UIScrollView platformScrollView)
288-
{
289-
return platformScrollView.AdjustedContentInset.InsetRect(platformScrollView.Bounds);
290-
}
291-
292292
Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
293293
{
294294
var scrollView = VirtualView;
@@ -301,18 +301,17 @@ Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double he
301301
return Size.Zero;
302302
}
303303

304-
var viewPortBounds = GetViewportBounds(platformScrollView);
305-
304+
var scrollViewBounds = platformScrollView.Bounds;
306305
var padding = scrollView.Padding;
307306

308307
if (widthConstraint == 0)
309308
{
310-
widthConstraint = viewPortBounds.Width;
309+
widthConstraint = scrollViewBounds.Width;
311310
}
312311

313312
if (heightConstraint == 0)
314313
{
315-
heightConstraint = viewPortBounds.Height;
314+
heightConstraint = scrollViewBounds.Height;
316315
}
317316

318317
// Account for the ScrollView Padding before measuring the content
@@ -331,28 +330,29 @@ Size ICrossPlatformLayout.CrossPlatformArrange(Rect bounds)
331330
var crossPlatformLayout = scrollView as ICrossPlatformLayout;
332331
var platformScrollView = PlatformView;
333332

333+
var contentSize = crossPlatformLayout.CrossPlatformArrange(bounds);
334+
334335
// The UIScrollView's bounds are available, so we can use them to make sure the ContentSize makes sense
335336
// for the ScrollView orientation
336-
var viewportBounds = GetViewportBounds(platformScrollView);
337-
338-
var contentSize = crossPlatformLayout.CrossPlatformArrange(viewportBounds.ToRectangle());
339-
337+
var viewportBounds = platformScrollView.Bounds;
340338
var viewportHeight = viewportBounds.Height;
341339
var viewportWidth = viewportBounds.Width;
342340
SetContentSizeForOrientation(platformScrollView, viewportWidth, viewportHeight, scrollView.Orientation, contentSize);
343341

344342
var container = GetContentView(platformScrollView);
345343

346-
if (container != null)
344+
if (container?.Superview is UIScrollView uiScrollView)
347345
{
348346
// Ensure the container is at least the size of the UIScrollView itself, so that the
349347
// cross-platform layout logic makes sense and the contents don't arrange outside the
350348
// container. (Everything will look correct if they do, but hit testing won't work properly.)
349+
350+
var scrollViewBounds = uiScrollView.Bounds;
351351
var containerBounds = contentSize;
352352

353353
container.Bounds = new CGRect(0, 0,
354-
Math.Max(containerBounds.Width, viewportBounds.Width),
355-
Math.Max(containerBounds.Height, viewportBounds.Height));
354+
Math.Max(containerBounds.Width, scrollViewBounds.Width),
355+
Math.Max(containerBounds.Height, scrollViewBounds.Height));
356356

357357
container.Center = new CGPoint(container.Bounds.GetMidX(), container.Bounds.GetMidY());
358358
}

0 commit comments

Comments
 (0)