1

I have a code for programmatic creation of buttons, and here it is

UIImage *aroundImage = [UIImage imageNamed:@"locBar.png"];
    UIImage *aroundImageDown = [UIImage imageNamed:@"locBarColor.png"];
    UIButton *aroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aroundButton setFrame:CGRectMake(240, 369, 80, 48)];  
    [aroundButton setImage:aroundImage forState:UIControlStateNormal];
    [aroundButton setImage:aroundImageDown forState:UIControlStateHighlighted];
    [aroundButton setImage:aroundImageDown forState:UIControlStateSelected];
    [aroundButton addTarget:self action:@selector(aroundMe) forControlEvents:UIControlEventTouchUpInside];    
    [self.view addSubview:aroundButton]; 

so now i have set the frame for the button, but what if this app is seen on iPhone4 in 640x960. Then i have different images "locBar@2x.png and locBarColor@2x.png" and they should be with a different CGRectMake, or should they? My question is how do I set up a button for both resolutions, and do that programmatically .

Spire
  • 1,055
  • 2
  • 17
  • 47
  • 1
    no prob whatever size given the pictures automatically adjusted in retina or simple iPhone display. but make sure the double size image with @2x is exist – Hiren Feb 09 '12 at 13:03
  • so my code is OK to use it for Iphone5>;What about the @HChouhan02 's answer? – Spire Feb 09 '12 at 13:10
  • he is not clear the question. and make sure it's not iPhone 5. it's a screen difference simple and retina like iPhone 2,3 ,3G Vs iPhone 4, 4s – Hiren Feb 09 '12 at 13:15

3 Answers3

2

it's automatic :) ios will load the @2x version if available and if the device has a x2 scale. The frame is in point, not in pixel.

Look here in Points Versus Pixels

Beuj
  • 602
  • 6
  • 20
1

If you include a high-resolution version of the image with the same name, but with @2x before the extension,iOS will automatically choose the correct resolution according to display resolution, as long as you use the [UIImage imageNamed:] function, or set the image in IB.

Mudit Bajpai
  • 3,010
  • 19
  • 34
-1

you have to put the condition and then according to the screen size make button frame and give them images.

hchouhan02
  • 948
  • 1
  • 8
  • 18