Here’s some code I grabbed from uibutton-in-code

objective-c btnImage = [UIImage imageNamed:@"image.png"]; [btnTwo setImage:btnImage forState:UIControlStateNormal];

In ruby, and with my own needs and wants and desires:

“`ruby okupimage = UIImage.imageNamed ‘okup.png’ okdownimage = UIImage.imageNamed ‘okdown.png’

@button = UIButton.buttonWithType(UIButtonTypeRoundedRect) @button.frame = [[0, 0], [51, 34]] @button.setImage(okupimage, forState:UIControlStateNormal) @button.setImage(okdownimage, forState:UIControlStateHighlighted) ”`

Here is another good reference, btw: learn-programming

Here are the images:

  • ok_up.png

    OK button, normal

  • ok_down.png

    OK button, pressed

Let’s see what this code does when I add it to my basic skeleton:

“`ruby def viewDidLoad okupimage = UIImage.imageNamed ‘okup.png’ okdownimage = UIImage.imageNamed ‘okdown.png’

@button = UIButton.buttonWithType(UIButtonTypeRoundedRect) @button.frame = [[134, 223], [51, 34]] @button.setImage(okupimage, forState:UIControlStateNormal) @button.setImage(okdownimage, forState:UIControlStateHighlighted)

view.addSubview(@button) end ”`

It ran on the first try - that never happens! So that was cool… here are screenshots. A neat feature is that in the simulator, a blue line appears when pressing the button.

  • ok_up_on_iphone.png

    okupon_iphone.png

  • ok_down_on_iphone.png

    okdownon_iphone.png

Wait, what!? wtf. Man, sometimes I think Cocoa/CocoaTouch is just full of bugs, and Apple tricks us into thinking it’s our fault. What could be wrong here!?

Fixthit!

The problem with the background is in my choice of “type”:

“`ruby

WRONG! @ok_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)

FIXED!

@ok_button = UIButton.buttonWithType(UIButtonTypeCustom) ”`

more buttons

“`ruby def viewDidLoad images = { ok: { up: UIImage.imageNamed(‘okup.png’), down: UIImage.imageNamed(‘okdown.png’), }, plus: { up: UIImage.imageNamed(‘plusup.png’), down: UIImage.imageNamed(‘plusdown.png’), }, minus: { up: UIImage.imageNamed(‘minusup.png’), down: UIImage.imageNamed(‘minusdown.png’), }, }

@minusbutton = UIButton.buttonWithType(UIButtonTypeRoundedRect) @minusbutton.frame = [[134 - 51, 223], [51, 43]] @minusbutton.setImage(images[:minus][:up], forState:UIControlStateNormal) @minusbutton.setImage(images[:minus][:down], forState:UIControlStateHighlighted)

@plusbutton = UIButton.buttonWithType(UIButtonTypeRoundedRect) @plusbutton.frame = [[134, 223], [51, 43]] @plusbutton.setImage(images[:plus][:up], forState:UIControlStateNormal) @plusbutton.setImage(images[:plus][:down], forState:UIControlStateHighlighted)

@okbutton = UIButton.buttonWithType(UIButtonTypeRoundedRect) @okbutton.frame = [[134 + 51, 223], [51, 34]] @okbutton.setImage(images[:ok][:up], forState:UIControlStateNormal) @okbutton.setImage(images[:ok][:down], forState:UIControlStateHighlighted)

view.backgroundColor = UIColor.grayColor view.addSubview(@plusbutton) view.addSubview(@minusbutton) view.addSubview(@ok_button) end ”`

  • buttons_up.png

    Screenshot of some UIButtons using images

  • buttons_down.png

    Screenshot of pushing buttons