UIImageView as button

后端 未结 5 1128
庸人自扰
庸人自扰 2020-12-12 17:48

I\'m trying to make a UIImageView accept actions and fire them everytime it clicked on the UIImageVie, but i\'m having hard time in getting it to work, please help me out

5条回答
  •  有刺的猬
    2020-12-12 18:08

    UIImageView is not a control, you can't add a target for control events to it. You have several options:

    Place an invisible UIButton over the UIImageView and add your target to that. You can set the button's style to custom and its text (and image) to nothing to make it invisible.

    Use just a UIButton. You can add a background image to it.

    Add a UITapGestureRecognizer to your image view and implement it's touch handler method.

    Subclass UIImageView and override the touch handling methods to do what ever you need to do.

提交回复
热议问题