Forum Xamarin.Forms

iOS ImageRenderer CreateNativeControl error

Poli97Poli97 Member ✭✭

I am trying to implement a custom ImageRenderer in iOS subclassing the native UIImageView, but I am having some problems with CreateNativeControl.

In the older Xamarin.Forms version (like 4.2) the custom native class that I initialized with protected override UIImageView CreateNativeControl() { return new NativeImage(); } looks like it never get called (the message I log in the constructor is not shown). The Custom Renderer is correctly initialized (the right message is logged).

In the latest stable version (like 4.4) in overriding of CreateNativeControl the return type it is said that has to be a FormsUIImageView, never heard of it, anyway I also tried to subclass that but same problem as before, it seems it never get called as the constructor message is not logged. The Custom Renderer is correctly initialized (the right message is logged).

Here the code I used:

`public class IOSImageView : ImageRenderer
{
public IOSImageView()
{
}

    protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
    {
        base.OnElementChanged(e);

        if(Control == null)
        {
            return;
        }

        Console.WriteLine("PIPPO created from Custom Renderer");  //this message is correctly logged
    }


    protected override UIImageView CreateNativeControl()  //FormsUIImageView  in XF 4.4
    {
        return new NativeImage();
    }

}

public class NativeImage : UIImageView  //FormsUIImageView in XF 4.4
{
    public NativeImage() : base()
    {
        Console.WriteLine("PIPPO created from native IOS");  //this message is NOT logged
    }

public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);
        Console.WriteLine("PIPPO touched");  //this (of course because no NativeImage is shown and there is no image to touch) is NOT logged
    }
}

}`

Best Answer

Answers

  • Poli97Poli97 Member ✭✭

    @ColeX said:
    In the package Forms 4.4 , CreateNativeControl method should return FormsUIImageView not UIImageView.

    FormsUIImageView inherits from UIImageView , change your subclass as

    public class NativeImage : FormsUIImageView {}
    

    Another way : use SetNativeControl, it is equivalent to CreateNativeControl .

      protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
    {
        base.OnElementChanged(e);
    
        if(Control == null)
        {
            return;
        }
    
        SetNativeControl(new NativeImage());
    
        Console.WriteLine("PIPPO created from Custom Renderer");  //this message is correctly logged
       }
    

    Ok thank you, with FormsUIImageView and SetNativeControl(new NativeImage()) it seems to work and the constructor messagge is correctly log, the problem is that the overridden methods (I tried for example with TouchesBegan that detect the touch) seems to NOT be called
    `public override void TouchesBegan(NSSet touches, UIEvent evt)
    {

            base.TouchesBegan(touches, evt);  //even if i comment that
    
            Console.WriteLine("PIPPO touched");  //message NOT logged
    
        }`
    

    When i tap on the image, nothing is logged, is it a problem of the emulator (I can't test on physical device at the moment) or am I doing something wrong?

  • ColeXColeX Member, Xamarin Team Xamurai

    Try to enable UserInteractionEnabled in constructor .

        public NativeImage()
        {
    
            this.UserInteractionEnabled = true;
            Console.WriteLine("PIPPO created from native IOS");  //this message is NOT logged
        }
    
  • Poli97Poli97 Member ✭✭

    @ColeX said:
    Try to enable UserInteractionEnabled in constructor .

        public NativeImage()
        {
    
            this.UserInteractionEnabled = true;
            Console.WriteLine("PIPPO created from native IOS");  //this message is NOT logged
        }
    

    Works great thank you! May I ask you why isn't this enabled by default?

  • ColeXColeX Member, Xamarin Team Xamurai
Sign In or Register to comment.