将UIImageView放在UIViewContentMode.ScaleAspectFit的屏幕上方

问题描述:

我已经将缩放比例设置为AspectFit,以便我的图像在UIImageView中显示。 但是我的图像出现在iphone屏幕的中心。将UIImageView放在UIViewContentMode.ScaleAspectFit的屏幕上方

我希望这将显示在我的屏幕顶部,保持其纵横比。这可以在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; 
     } 
    }  
} 
+0

你为什么初始化方法ImageView的画?是否ImageView的有父视图? –

+0

@ColeXia那是因为,我有我的Customcontrol中的这个ImageView:UIView,我将在xamarin窗体中使用它。我将从窗体动态地改变我的图像。我接受任何其他建议。 – Praddy

+0

你的意思是customRenderer?请提供更多代码CustomControl –

使用的OnDraw方法把它画CGImage达到了这个。

Draw Image in UIView Xamarin.IOS C#