如何编写iphone 3.5英寸屏幕和4英寸屏幕的代码 [英] how can i write the code for iphone 3.5 inches screen and 4 inches screen

查看:79
本文介绍了如何编写iphone 3.5英寸屏幕和4英寸屏幕的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款应用。在应用程序中,我编写了3.5英寸屏幕的代码。我拿了一个视图控制器;在这个视图控制器中我想显示两个图像,一个用于顶部(Imagelogo),另一个用于底部(图像构建)。在3.5英寸的屏幕上,显示器没有问题。但在4英寸屏幕上,存在显示问题。这两个图像无法正确显示。我不知道如何为3.5英寸和4英寸屏幕编写相同的代码。我可以使用宏吗?任何人都请给我一些想法。我是编程新手。提前致谢。

I am developing an app. In the app I wrote the code for a 3.5-inch screen. I took one view controller; in this view controller I want to display two images, one for the top (Imagelogo) and another for the bottom (Image Building). On the 3.5-inch screen there is no problem with the display. But on the 4-inch screen, there is a display problem. The two images are not displayed properly. I don't know how to write the same code for both the 3.5 and 4-inch screen. Can I use macros? Please give me ideas, anyone. I am new to programming. Thanks in advance.

以下是我的代码。

Viewcontroller.m(3.5英寸屏幕)

Viewcontroller.m (3.5 inches screen)

此图片将显示在顶部。

imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10, 162, 57)];
imgLogo.image=[UIImage imageNamed:@"Logo-01.png"];
[self.view addSubview:imgLogo];

此图片将显示在底部

imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 320, 320,140 )];
imgBuilding.image=[UIImage imageNamed:@"image-02.png"];
[self.view addSubview:imgBuilding];


推荐答案

在Viewcontroller.h中定义float scaleY;

In your "Viewcontroller.h" define float scaleY;

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
   float scaleY;
}
@end

在Viewcontroller.m课程中

In your "Viewcontroller.m" class

在[super viewDidLoad]之后的viewDidLoad方法中;将您的scaleY值设置为iPhone系列中不同的屏幕尺寸设备。

in your viewDidLoad method after [super viewDidLoad]; put your scaleY value for your different screen size devices in iPhone family.

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        CGFloat scale = [UIScreen mainScreen].scale;
        result = CGSizeMake(result.width * scale, result.height * scale);
        NSLog(@"Height ........%f",result.height);

        if(result.height == 480 || result.height == 960)
        {
            // iPhone 3.5 inches Screen
            scaleY=1.0;

        }
        else if(result.height == 1136)
        {
            // iPhone 4 inches Screen
            scaleY=1.18333f;

        }
    }
}

然后乘以scaleY值与视图的y位置。

Then multiply the scaleY value with your "y" position of the views.

UIImageView *imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10*scaleY, 162, 57)];
imgLogo.image=[UIImage imageNamed:@"Logo-01.png"];
[self.view addSubview:imgLogo];

UIImageView *imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 320*scaleY, 320, 140 )];
imgBuilding.image=[UIImage imageNamed:@"image-02.png"];
[self.view addSubview:imgBuilding];

您可以在整个代码中使用此scaleY值(如上所示)将其乘以您的y相应视图的位置。由于iPhone家庭设备的高度不同,所以我们只能改变他们的y位置。

You can use this "scaleY" value (as shown above) throughout your code to multiply it with your y position of the corresponding views. Since iPhone Family Devices are different in heights so we can only change their "y" position.

希望这会有所帮助。

这篇关于如何编写iphone 3.5英寸屏幕和4英寸屏幕的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆