question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In .Net MAUI a container is wrong resized on iOS, when an image with AspectFit is inside

See original GitHub issue

This issue has been moved from a ticket on Developer Community.


Hi, in .Net MAUI a container is wrong resized on iOS, when an image with AspectFit is inside. I described it with pictures in the word file. You 'll find in the attachments also a sample programm to debug the problem. The error cause, that I can’t update my Xamarin.Forms app to .NET MAUI. Thanks Marian

(full repro attached in internal ticket)

From the Word document:

image

image

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiAppImages2.MainPage">

    <ScrollView>
        <VerticalStackLayout Spacing="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

            <Grid>
                <Image
                    Source="image1.png"
                    Aspect="AspectFit"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="Start"
                    BackgroundColor="Yellow"/>
                <Border HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Stroke="Red" StrokeThickness="4" StrokeShape="RoundRectangle 10,10,10,10" BackgroundColor="Transparent" Margin="0" Padding="0" />
            </Grid>


            <Label
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />

            <Label
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>



Original Comments

Feedback Bot on 3/15/2023, 07:56 PM:

(private comment, text removed)


Original Solutions

(no solutions)

Issue Analytics

  • State:open
  • Created 6 months ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
marcoseraphincommented, Apr 18, 2023

Hi all,

yes I am also experience the same behavior with my images in my current project, so please fix the problem.

1reaction
davidortinaucommented, Jul 20, 2023

Here’s a fundamental workaround. The sizing logic in the GetDesiredSize override could be improved, but this works for this case and gives you the place to do whatever you need for sizing.

Simulator Screenshot - iPhone 14 Pro - 2023-07-20 at 14 01 30

Subclass ImageHandler for iOS, and then register the handler for iOS Image.

Platforms/iOS/SizedImageHandler.cs

using System;
using Microsoft.Maui.Handlers;

namespace MauiAppImages2.Platforms.iOS;

public class SizedImageHandler : ImageHandler
{
	public SizedImageHandler()
	{
	}

    public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
    {
        if (VirtualView.Aspect == Aspect.AspectFit
            && VirtualView.VerticalLayoutAlignment != Microsoft.Maui.Primitives.LayoutAlignment.Fill
            && double.IsInfinity(heightConstraint))
        {
            if (PlatformView.Image != null)
            {                
                var ratio = widthConstraint / this.PlatformView.Image.PreferredPresentationSizeForItemProvider.Width;
                heightConstraint = Math.Round(this.PlatformView.Image.PreferredPresentationSizeForItemProvider.Height * ratio);
            }
        }

        return new(widthConstraint, heightConstraint);
    }
}

MauiProgram.cs

using Microsoft.Extensions.Logging;

namespace MauiAppImages2;

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			.ConfigureFonts(fonts =>
			{
				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			});

#if IOS
		builder.ConfigureMauiHandlers((handlers) =>
		{
			handlers.AddHandler<Image, Platforms.iOS.SizedImageHandler>();
		});

#endif

#if DEBUG
		builder.Logging.AddDebug();
#endif


		return builder.Build();
	}

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

NET MAUI Image wrong size - xamarin
I made some changes: I tried using data binding in MVVM way. I tried counting image ratio using platform code. The following is...
Read more >
Image - .NET MAUI
NET MAUI Image displays an image that can be loaded from a local file, a URI, ... At build time, vector images are...
Read more >
Image Scaling in .NET MAUI
The Image control has an Aspect property which allow us to indicate how an image will fit into the display area (scaling). This...
Read more >
How to resize an UIImageView to fit a container view using ...
An image view changes its parent frame instead of resizing itself to fit the parent frame. The problem might be hard to understand...
Read more >
Sizing an image to the width of screen and keep the ratio?
I've tried aspect fit but it creates white space on the top and bottom. ... The imageview is not resizing, just the image...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found