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 } }
}`
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 }
Answers
In the package Forms 4.4 , CreateNativeControl method should return
FormsUIImageView
notUIImageView
.FormsUIImageView inherits from UIImageView , change your subclass as
Another way : use
SetNativeControl
, it is equivalent toCreateNativeControl
.Ok thank you, with
FormsUIImageView
andSetNativeControl(new NativeImage())
it seems to work and the constructor messagge is correctly log, the problem is that the overridden methods (I tried for example withTouchesBegan
that detect the touch) seems to NOT be called`public override void TouchesBegan(NSSet touches, UIEvent evt)
{
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?
Try to enable
UserInteractionEnabled
in constructor .Works great thank you! May I ask you why isn't this enabled by default?
This property of
UIImageView
isfalse
by default , check https://developer.apple.com/documentation/uikit/uiimageview/1621063-userinteractionenabled?language=objc