Skip to content
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

[release/8.0.1xx-preview3] Make sure that we have valid values for setting the ContentSize of the ScrollView on iOS #14242

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ public static void MapVerticalScrollBarVisibility(IScrollViewHandler handler, IS

public static void MapOrientation(IScrollViewHandler handler, IScrollView scrollView)
{
if (GetContentView(handler.PlatformView) is ContentView currentContentContainer)
if (handler?.PlatformView is UIScrollView uiScrollView)
{
currentContentContainer.SetNeedsLayout();
var fullContentSize = scrollView.PresentedContent?.DesiredSize ?? Size.Zero;
var viewportWidth = uiScrollView.Bounds.Width;
var viewportHeight = uiScrollView.Bounds.Height;
SetContentSizeForOrientation(uiScrollView, viewportWidth, viewportHeight, scrollView.Orientation, fullContentSize);
}
}

Expand Down Expand Up @@ -198,9 +201,12 @@ static Func<Rect, Size> ArrangeScrollViewContent(Func<Rect, Size> internalArrang
Math.Max(containerBounds.Height, scrollViewBounds.Height));
container.Center = new CGPoint(container.Bounds.GetMidX(), container.Bounds.GetMidY());
}

var contentSize = internalArrange(rect);
var size = SetContentSizeForOrientation(platformScrollView, scrollView.Orientation, contentSize);
return size;

SetContentSizeForOrientation(platformScrollView, platformScrollView.Bounds.Width, platformScrollView.Bounds.Height, scrollView.Orientation, contentSize);

return contentSize;
};
}

Expand Down Expand Up @@ -258,16 +264,19 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra
widthConstraint = AccountForPadding(widthConstraint, padding.HorizontalThickness);
heightConstraint = AccountForPadding(heightConstraint, padding.VerticalThickness);

var crossPlatformSize = virtualView.CrossPlatformMeasure(widthConstraint, heightConstraint);
var crossPlatformContentSize = virtualView.CrossPlatformMeasure(widthConstraint, heightConstraint);

// Add the padding back in for the final size
crossPlatformSize.Width += padding.HorizontalThickness;
crossPlatformSize.Height += padding.VerticalThickness;
crossPlatformContentSize.Width += padding.HorizontalThickness;
crossPlatformContentSize.Height += padding.VerticalThickness;

var viewportWidth = Math.Min(crossPlatformContentSize.Width, widthConstraint);
var viewportHeight = Math.Min(crossPlatformContentSize.Height, heightConstraint);

var size = SetContentSizeForOrientation(platformView, virtualView.Orientation, crossPlatformSize);
SetContentSizeForOrientation(platformView, widthConstraint, heightConstraint, virtualView.Orientation, crossPlatformContentSize);

var finalWidth = ViewHandlerExtensions.ResolveConstraints(size.Width, virtualView.Width, virtualView.MinimumWidth, virtualView.MaximumWidth);
var finalHeight = ViewHandlerExtensions.ResolveConstraints(size.Height, virtualView.Height, virtualView.MinimumHeight, virtualView.MaximumHeight);
var finalWidth = ViewHandlerExtensions.ResolveConstraints(viewportWidth, virtualView.Width, virtualView.MinimumWidth, virtualView.MaximumWidth);
var finalHeight = ViewHandlerExtensions.ResolveConstraints(viewportHeight, virtualView.Height, virtualView.MinimumHeight, virtualView.MaximumHeight);

return new Size(finalWidth, finalHeight);
}
Expand Down Expand Up @@ -304,27 +313,19 @@ static double AccountForPadding(double constraint, double padding)
return Math.Max(0, constraint - padding);
}

static Size SetContentSizeForOrientation(UIScrollView platformScrollView, ScrollOrientation orientation, Size crossPlatformSize)
{
CGRect scrollViewBounds = platformScrollView.Bounds;
var contentSize = AccountForOrientation(crossPlatformSize, scrollViewBounds.Width, scrollViewBounds.Height, orientation);
platformScrollView.ContentSize = contentSize;
return contentSize;
}

internal static Size AccountForOrientation(Size size, double widthConstraint, double heightConstraint, ScrollOrientation orientation)
static void SetContentSizeForOrientation(UIScrollView uiScrollView, double viewportWidth, double viewportHeight, ScrollOrientation orientation, Size contentSize)
{
if (orientation is ScrollOrientation.Vertical or ScrollOrientation.Neither)
{
size.Width = widthConstraint;
contentSize.Width = Math.Min(contentSize.Width, viewportWidth);
}

if (orientation is ScrollOrientation.Horizontal or ScrollOrientation.Neither)
{
size.Height = heightConstraint;
contentSize.Height = Math.Min(contentSize.Height, viewportHeight);
}

return size;
uiScrollView.ContentSize = contentSize;
}
}
}