UITapGestureRecognizer not working with custom view derived from UIView.

PreranaPPreranaP USUniversity

Hello All,


I have an Custom UIView which contains an UImageView, UILabel. The view is created extending UIView in an custom class and adding all the functionality in the specific class as follows :

                public class MyCustomView : UIView
              {
                 UIImageView image,videoicon;
                 UILabel title,subtile;
                 UIView subview;
                 nfloat viewX,viewY,viewHeight,viewWidth;
                public nfloat padding { get; set;}
                     //Methods used for UI modification
               }



I create different objects of this class and set different images and labels accordingly. Now I want to attach UITapGestureRecognizer on the entire UIView as per the object creation. I implemented the following approach :

                 public void AttachEventToImage()
            {
        tabTile1 = new UITapGestureRecognizer ();
        tabTile1.AddTarget (() => 

            Console.WriteLine("Image Clicked")

        );
        tabTile1.NumberOfTapsRequired = 1;
        tabTile1.DelaysTouchesBegan = true;
        viewTile1.AddGestureRecognizer (tabTile1);          
    }

where viewTile1= new MyCustomView(0,0,this.View.Bounds.Width-2*padding,252,padding);


The above way does not work in case of custom UIView whereas the same approach works with a simple UIView.


On debugging, following were my findings :


1) In case of normal UIView, the view object after attaching gestureRecogniser looks like the following :


image


Here the recognizer object is attached and is visible.



2.) In case of custom UIView, initially the view object looks like the following


image


but after I resize the frame of the same view object, the gesturerecogniser object is no longer visible as follows :


image




Can there be a special case to be handled in case of custom UIView? It works well if I take 6 different UIViews with 6 images and uilabels respectively but I want to make a custom control using the UIView in order to reuse it. Is there anything missing or should I use a different approach to handle the view click event in case of custom view?


Any help or pointers will be highly appreciated!! Thanks a ton in advance.

Posts

  • xxfxxf USMember

    Set your UIView's UserInteractionEnabled=true

  • PreranaPPreranaP USUniversity

    Hi @xxf , sorry I forgot to mention in the post, yes I had set UIView's UserInteractionEnabled=true as following after object creation but no luck :(

                       viewTile1= new MyCustomView(0,0,this.View.Bounds.Width-2*padding,252,padding);
                viewTile1.BackgroundColor = UIColor.Blue;
                viewTile1.UserInteractionEnabled = true;
    


  • TonyDTonyD USMember ✭✭✭

    @PreranaP hi - did you ever resolve this?

Sign In or Register to comment.