簡體   English   中英

我怎樣才能為iphone 3.5英寸屏幕和4英寸屏幕編寫代碼

[英]how can i write the code for iphone 3.5 inches screen and 4 inches screen

我正在開發一個應用程序。 在應用程序中,我編寫了3.5英寸屏幕的代碼。 我拿了一個視圖控制器; 在這個視圖控制器中,我想顯示兩個圖像,一個用於頂部(Imagelogo),另一個用於底部(圖像構建)。 在3.5英寸的屏幕上,顯示器沒有問題。 但在4英寸屏幕上,存在顯示問題。 這兩個圖像無法正確顯示。 我不知道如何為3.5英寸和4英寸屏幕編寫相同的代碼。 我可以使用宏嗎? 任何人都請給我一些想法。 我是編程新手。 提前致謝。

以下是我的代碼。

Viewcontroller.m(3.5英寸屏幕)

此圖像將顯示在頂部。

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

您可以在界面構建器中使用autoResize功能,也可以通過代碼使用它。

如果您的應用支持IOS 5及更高版本,您可以使用Apple的自動布局系統。

您可以通過代碼根據屏幕尺寸放置圖像

if ([[UIScreen mainScreen] bounds].size.height == 568)
{
       //iphone 5 image 
}else
{

}

在你的“Viewcontroller.h”中定義float scaleY;

#import <UIKit/UIKit.h>

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

在“Viewcontroller.m”類中

在[super viewDidLoad]之后的viewDidLoad方法中; 將您的scaleY值放在iPhone系列中的不同屏幕尺寸設備上。

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”位置相乘。

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”位置。

希望這可以幫助。

試試像這樣的autoresizingmask。

 UIImageView *imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10, 162, 57)];


    imgLogo.image=[UIImage imageNamed:@"Logo-01.png"];
    [imgLogo setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)];

    [self.view addSubview:imgLogo];

這是您的問題的快速解決方案,但我建議使用自動布局。

 if ([[UIScreen mainScreen] bounds].size.height == 568)
    {    
         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, 420,140 )];
       imgBuilding.image=[UIImage imageNamed:@"image-02.png"];
       [self.view addSubview:imgBuilding];            
    }
    else
    {
       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];     
    }

希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM