Domanda

In objective C, one can set a background image to a stretched png like so:

button = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
[button setTitle: @"Tap me" forState: UIControlStateNormal];
[button setBackgroundImage:[[UIImage imageNamed: @"greenButton.png"]
                                 stretchableImageWithLeftCapWidth:8.0f
                                 topCapHeight:0.0f]
                       forState:UIControlStateNormal];

Trying to transpose this over to Ruby, I keep getting exceptions though. The problem is with the two methods called on the UIImage instance: stretchableImageWithLeftCapWidth and topCapHeight.

I've tried the following to no avail:

greenImage = UIImage.imageNamed("greenButton.png")
greenImage.stretchableImageWithLeftCapWidth = 8.0
greenImage.topCapHeight = 0.0
@timerButton.setBackgroundImage(greenImage, forState: UIControlStateNormal)

Can anyone advise?

È stato utile?

Soluzione

You have incorrectly broken that method selector up

It is declared as

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

it should be called like this

greenImage.stretchableImageWithLeftCapWidth(8.0, topCapHeight:0.0)

You'll most likely want to assign that to something so it might look like this

greenImage = UIImage.imageNamed("greenButton.png")
greenImage = greenImage.stretchableImageWithLeftCapWidth(8.0, topCapHeight:0.0)
@timerButton.setBackgroundImage(greenImage, forState: UIControlStateNormal)

Side note

You are correct the method is marked as newly deprecated in iOS 5, but it's also important to note that the replacement method was also introduced in iOS 5 so if you plan to support older iOS's then you will need to continue using this.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top