首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UIViewContentMode.ScaleAspectFit将UIImageView置于屏幕顶部

使用UIViewContentMode.ScaleAspectFit将UIImageView置于屏幕顶部
EN

Stack Overflow用户
提问于 2017-08-28 19:37:42
回答 1查看 254关注 0票数 0

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

我希望这是显示在我的屏幕顶部,保持其纵横比。这可以在C#中完成吗?这就是我目前设置图像的方式。

代码语言:javascript
复制
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:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
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;
        }
    }
}

我将表单控件设置为:

代码语言:javascript
复制
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;
        }
    }    
}
EN

回答 1

Stack Overflow用户

发布于 2017-08-29 17:05:41

这是通过使用OnDraw方法将其绘制为CGImage来实现的。

Draw Image in UIView Xamarin.IOS C#

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45918078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档