How do you make an UIImageView on the storyboard clickable (swift)

前端 未结 7 673
后悔当初
后悔当初 2020-12-13 02:39

I am new to swift (and Xcode development in general) and I was wondering how to make an ImageView on the storyboard clickable. What I\'m trying to do is make it so when its

7条回答
  •  我在风中等你
    2020-12-13 02:55

    You can do it even easier and make a image clickable via Storyboard with no coding at all.

    • First you need to drag a UITapGestureRecognizer onto your UIImageView in Storyboard.
    • Then you create the IBAction you want to run in your code with @IBAction func imageClicked(_ sender: Any) {}
    • Next you need to connect the UITapGestureRecognizer to the IBAction in your class by selecting the gesture recognizer in the Document Outline, then switching to the Connection Inspector Tab and dragging the Sent Actions -> Selector to your UIViewController where you then select the appropriate action you created previously.
    • Finally you have to set the checkbox User Interaction Enabled on the image view.

    Done, a fully clickable UIImageView without writing a single line of code except the obvious function you want to invoke. But hey, if you for example want to push a segue instead, then you can go without coding at all by using the gesture recognizers Triggered Segues instead of its Sent Actions.

    Though Storyboard have their limitations, theres no need to write code for clickable images. ;)

提交回复
热议问题