0

I am trying to use a custom image as a button in my app. How do I add an action for an image?

This is for an iOS app and I don't want to use the default text button as a button. I've already tried control+dragging the image to the ViewController but there is no "Action" option.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let designChoice = designPrac.randomElement()
    let forChoice = forWhatPrac.randomElement()
    let helpChoice = toHelpPrac.randomElement()

    designLabel.text = designChoice
    forLabel.text = forChoice
    helpLabel.text = helpChoice
}

This is the code I have to run when the app launches but I can't find a way to get this to work for when my image button is clicked.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
bluewizard3
  • 19
  • 1
  • 1
  • 7
  • 2
    What image? The code you posted doesn't seem to have anything to do with your question? Why not use `UIButton` with your image? – rmaddy Jan 30 '19 at 15:05
  • have you tried making a UIButton the same size on the image and just removing the "button" text? have you tried a tap gesture on the UIImageView? – StartPlayer Jan 30 '19 at 15:06
  • I'm not sure what you mean by "use UIButton" with your image. I am new to Swift sorry for the ignorance. I know how to add a button to the storyboard but not how to make it the image I have in Assets.xcassets. How do I use tap gesture? – bluewizard3 Jan 30 '19 at 15:09
  • @bluewizard3 A normal UIButton can have an image instead of a title. – rmaddy Jan 30 '19 at 15:13
  • https://stackoverflow.com/questions/27880607/how-to-assign-an-action-for-uiimageview-object-in-swift/53809261#53809261 -> add tap gesture on imageview and handle action of imageview and also we can use all the child of UIView. – Ravindra_Bhati Jan 31 '19 at 17:47

3 Answers3

0

In order to add an action to an UIImageView, you need to add a UITapGestureRecognizer to it, you can do it like this:

<#YourImageView#>.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleImageTap)))

Then you can handle the tap using the selector:

@objc func handleImageTap() {
    // handle image tap here    
}
Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
0

You can use addGestureRecognizer for this purpose:

let imageView = UIImageView()
//add necessary code 
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(MethodForAction)))
shakil080
  • 351
  • 2
  • 5
-1

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

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    // create tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

    // add it to the image view;
    imageView.addGestureRecognizer(tapGesture)
    // make sure imageView can be interacted with by user
    imageView.isUserInteractionEnabled = true
}

func imageTapped(gesture: UIGestureRecognizer) {
    // if the tapped view is a UIImageView then set it to imageview
    if (gesture.view as? UIImageView) != nil {
        print("Image Tapped")
        //Here you can initiate your new ViewController

    }
}
Nolan Gelinas
  • 53
  • 1
  • 8