0

How would I make a UIButton and assign an image to it, without text and than simply connect a IBAction? I am new to swift and can't use the storyboard with my game. I would really appreciate help. Thank you

Haden Yo
  • 31
  • 4
  • Drag and drop your images to Xcode project assets. Now drag and drop an image to your view. In the attribute inspector, check for the image attribute and select the image name from drop down. Remove the label named "Button", to remove the text. – triandicAnt Jun 15 '16 at 02:43
  • Have you tried [this](http://stackoverflow.com/questions/24064740/create-a-button-programmatically-and-set-a-background-image)? :) – Lucas Jun 15 '16 at 02:53

2 Answers2

0

You can just use UIImage, and connect inside your storyboard. Thereafter, drag and drop a tap gesture recogniser and do whatever your require with it.

enter image description here

jo3birdtalk
  • 616
  • 5
  • 20
0

I think something like this would help you create UIButton with image and perform action on its click.

let aBtn   = UIButton(type: UIButtonType.Custom) as UIButton
aBtn.frame = CGRectMake(x , y, width, height)
aBtn.setBackgroundImage(UIImage(named: "your image"), forState: .Normal)
aBtn.addTarget(self, action:"buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(aBtn)

And you can perform certain function after button click here.

func buttonClicked(sender: UIButton){
    //your code here
}
mshrestha
  • 788
  • 5
  • 14