0

On the front page of my app I have two images, which are laid over a portion of two buttons. The buttons are automatically re-scaled (via depending on the size of the iPhone screen. Unfortunately when the images are re-scaled it ends up being pretty ugly.

Can I dictate the size and location of the two images (via code), depending on the size of the iphone screen? If so how would I go about doing so?

Thanks for helping a novice out!

Update 13MAR13

This is the code I tried before. It causes no errors, but no images appear on the page!

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        UIImageView *myImageView = [[UIImageView alloc]initWithFrame: CGRectMake(220, 2.5, 95, 75)];
        myImageView.image = [UIImage imageNamed:@"BFS.png"];
        UIImageView *myImageViewtwo = [[UIImageView alloc]initWithFrame: CGRectMake(300, 100, 95, 75)];
        myImageViewtwo.image = [UIImage imageNamed:@"RMS.png"];


    }
    if(result.height == 568)
    {
        UIImageView *myImageView = [[UIImageView alloc]initWithFrame: CGRectMake(100, 400, 95, 75)];
        myImageView.image = [UIImage imageNamed:@"BFS.png"];
        UIImageView *myImageViewtwo = [[UIImageView alloc]initWithFrame: CGRectMake(100, 300, 95, 75)];
        myImageViewtwo.image = [UIImage imageNamed:@"RMS.png"];

    }
}
Andy A
  • 236
  • 3
  • 12
  • Did you read it? http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/MobileHIG/IconsImages/IconsImages.html – tuffkid Mar 12 '13 at 21:19
  • If you need to change the background image depending on whether it is an iPhone 4 or 5, check out http://stackoverflow.com/questions/12446990/how-to-detect-iphone-5-widescreen-devices – Niro Mar 12 '13 at 21:24

1 Answers1

3

You can take 2 different imags & UIImageViews behind transparent buttons and set their sizes according to screen size.

Code for get screen size:

int h = [[UIScreen mainScreen] bounds].size.height;

Then, check

if([[UIScreen mainScreen] bounds].size.height == 480) { 

  imgVw1.frame = CGRectMake(x, y, w, h);
  imgVw1.image = [UIImage imagenamed:@"....png"];
} //iPhone-4

if([[UIScreen mainScreen] bounds].size.height == 568) {

  imgVw1.frame = CGRectMake(x, y, w, h);
  imgVw1.image = [UIImage imagenamed:@"...@2x.png"];
} //iPhone-5

You can take two images for it.

  1. imgButton.png -> Normal
  2. imgButton@2x.png -> Retina Display


Edited Code

In .h file,

@property(nonatomic, retain) IBOutlet UIButton *btn;

In. m file,

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;

    if(result.height == 480)
    {
        [btn setFrame:CGRectMake(75, 20, 175, 100)];
        [btn setImage:[UIImage imageNamed:@"Img_Circle.png"] forState:UIControlStateNormal];
    }
    if(result.height == 568)
    {
        [btn setFrame:CGRectMake(65, 60, 200, 120)];
        [btn setImage:[UIImage imageNamed:@"Img_Circle@2x.png"] forState:UIControlStateNormal];
    }
}

As we already declared property in .h file, then no need to alloc again here.
Just simply set frames for it.

Hopefully, this will help to you.
Thanks.

Manann Sseth
  • 2,745
  • 2
  • 30
  • 50
  • Dear iManan, thank you for your answer! I implemented your code but it created some errors - imgVw1 (use of undeclared identifier). I'm sure this is a stupid oversight on my behalf - could you help me a little more? I also updated my original question with some code I tried before. It causes no errors, but no pictures appear! Just for my own future reference, could you tell me what was wrong with it? – Andy A Mar 13 '13 at 21:08
  • Check my edit code. hopefully, it'll help you. Thnaks – Manann Sseth Mar 14 '13 at 04:20