1
votes

I am working to convert a Xamarin Forms View into a Native UIView using the below code snippet. But the size of view could not be get from the Xamarin forms View, and also the subviews could not be auto resized when the view is Resized.

I have tried using AutoResizingMask but it did not worked.Any help is much appreciated.

public void CustomView(int pageIndex, View customView, Point offset)
{
 var visualElement = Platform.CreateRenderer(customView);
 visualElement.NativeView.Frame = new CGRect((int)offset.X, (int)offset.Y, 
 (int)100, (int)100);
 visualElement.NativeView.AutoresizingMask = 
 UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
 visualElement.NativeView.ContentMode = UIViewContentMode.ScaleToFill;
 visualElement.NativeView.AutosizesSubviews = true;
 visualElement.Element.Layout(new CGRect((int)offset.X, (int)offset.Y, 
 (int)100, (int)100).ToRectangle());
 visualElement.Element.AutoresizingMask = UIViewAutoresizing.All;
 visualElement.NativeView.AutosizesSubviews = true;
 }

CustomView passed on the above:

Grid grid = new Grid();
        grid.HeightRequest = 400;
        grid.WidthRequest = 300;
        grid.BackgroundColor = Color.Yellow;            
        Image customImage = new Image();
        customImage.VerticalOptions = LayoutOptions.FillAndExpand;
        customImage.HorizontalOptions = LayoutOptions.FillAndExpand;            
        customImage.Source = "Approved.png";
        grid.Children.Add(customImage);

I have tried to resize the NativeView after conversion, but image is not resizing according to the layout change.

1

1 Answers

0
votes

We don't use AutoResizingMaskanymore since auto Layout comes out .

Try to modify your code

public void CustomView(int pageIndex, View customView, Point offset)
    {
        var visualElement = Platform.CreateRenderer(customView);

        var view = visualElement.NativeView;
        var margins = View.LayoutMarginsGuide;// Get the parent view's layout
        view.LeadingAnchor.ConstraintGreaterThanOrEqualTo(margins.LeadingAnchor,(nfloat)offset.X).Active = true;
        view.TopAnchor.ConstraintGreaterThanOrEqualTo(margins.TopAnchor,(nfloat)offset.Y).Active = true;

        view.HeightAnchor.ConstraintEqualTo(100).Active = true;
        view.WidthAnchor.ConstraintEqualTo(100).Active = true;
    }