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"];

    }
}
4

1 に答える 1

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.

于 2013-03-13T11:03:33.953 に答える