使用UIViewContentMode.ScaleAspectFit将UIImageView放在屏幕顶部

时间:2017-08-28 11:37:42

标签: xamarin xamarin.ios xamarin.forms

我已将比例设置为AspectFit,以便我的图像显示在UIImageView中。 但是我的图像出现在iphone的屏幕中央。

我想让它显示在我的屏幕顶部,保持其宽高比。这可以在C#中完成。这就是我目前设置图像的方式。

public override void Draw(CGRect rect)
{
    myImageView = new UIImageView()
    {
        Image = UIImage.FromBundle("filename"),
        ContentMode = UIViewContentMode.ScaleAspectFit,
        Frame = new CGRect(0, 0, rect.Width, rect.Height)
    };
}

任何代码示例或建议都会有所帮助。 感谢。

我的CustomControl:

[DesignTimeVisible(true), System.ComponentModel.Category("CustomControl")]
public partial class CustomControl : UIView
{
    [Export("ImageFileName"), Browsable(true)]
    public string ImageFileName{get;set;}

    public static KipperView Create()
    {
        var arr = NSBundle.MainBundle.LoadNib("CustomControl", null, null);
        var view = Runtime.GetNSObject<KipperView>(arr.ValueAt(0));
        return view;
    }

    public CustomControl() : base()
    {}

    public CustomControl(IntPtr p) : base(p)
    {}

    public override void Draw(CGRect rect)
    {
        myImageView = new UIImageView()
        {
            Image = UIImage.FromBundle(ImageFileName),
            ContentMode = UIViewContentMode.ScaleAspectFit,
            Frame = new CGRect(0, 0, rect.Width, rect.Height)
        };
        this.Add(myImageView);
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);
        UITouch touch = touches.AnyObject as UITouch;
        var x = (float)touch.GetPreciseLocation(myImageView).X;
        var y = (float)touch.GetPreciseLocation(myImageView).Y;
    }
}

我的CustomRendered:

public class CustomRendered : ViewRenderer<MyFormsControl, CustomControl>
{
    protected override void OnElementChanged(ElementChangedEventArgs<MyFormsControl> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
        {
            var control = new CustomControl();
            SetNativeControl(control);
        }
        if (this.Element == null) return;
        Control.ImageFileName = "logo.png";
        Control.ContentMode = UIViewContentMode.ScaleAspectFit;
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        switch (e.PropertyName)
        {
            case "ImageFileName":
                Control.ImageFileName = "xamarin.png";
                this.SetNeedsDisplay();
                break;
            default:
                return;
        }
    }
}

我将表单控件设置为:

public class MyFormControl : View
{
    public static readonly BindableProperty ImageNameProperty = BindableProperty.Create(nameof(ImageName), typeof(string), typeof(CustomKipperControl), null, propertyChanged: OnItemsSourcePropertyChanged);

    private static void OnItemsSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue) { }

    public string ImageName
    {
        get
        {
            return (string)GetValue(ImageNameProperty);
        }
        set
        {
            SetValue(ImageNameProperty, value);
        }
    }

    protected override void OnPropertyChanged(string propertyName)
    {
        base.OnPropertyChanged(propertyName);
        switch (propertyName)
        {
            case "ImageFileName":
                break;
        }
    }    
}

1 个答案:

答案 0 :(得分:0)

使用OnDraw方法将其绘制为CGImage。

Draw Image in UIView Xamarin.IOS C#

相关问题