不同ios设备的对齐方式

时间:2013-11-15 10:48:57

标签: ios iphone alignment

我是iOS编程新手。我申请了4英寸的iPhone视网膜。现在我想自定义我的代码,以便在iPhone视网膜3.5英寸和iPhone 5上运行。

我尝试为self.view.frame.size.widthself.view.frame.size.height创建if查询,但我收到“Not assignable”错误。

有人能告诉我如何在所有设备中指定设置工作区的条件吗?如果可能的话,请告诉我所有设备的确切屏幕大小。

6 个答案:

答案 0 :(得分:2)

您可以使用:

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size; 
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

参考文献: https://developer.apple.com/library/ios/documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html

答案 1 :(得分:2)

You look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.

Here's some code to get the screen size:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

Be aware that width and height might be swapped, depending on device orientation.

答案 2 :(得分:0)

比较屏幕大小包括此宏并在必要时使用它

    #define isPhone5 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)


如果您只想比较屏幕尺寸,请将上面的宏缩小为


    #define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE


例如,在.m文件中

    #import "ViewController.h"

    #define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE //hear u put the macro and use it anywhere in this file

 @interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate

 @end

 @implementation ViewController

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     if(isiPhone5)
     {
        NSLog(@"I am 4inch screen");

     }
     else
     {
         NSLog(@"I am 3.5inch screen");
     }
     // Do any additional setup after loading the view, typically from a nib.
 }


答案 3 :(得分:0)

试试这个.M文件:

-(void)viewWillAppear:(BOOL)animated
{

    [self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)

    {
        [self setFrameForLeftAndRightOrientation];
    }

    else if(orientation == UIInterfaceOrientationPortrait )

    {
        [self setFrameForPortraitAndUpsideDownOrientation];
    }
}

- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
                                         duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)

    {
        [self setFrameForLeftAndRightOrientation];
    }

    else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)

    {
        [self setFrameForPortraitAndUpsideDownOrientation];
    }


}
-(void)setFrameForLeftAndRightOrientation
{

    if(IS_IPAD)

    {
        //Write here code for iPad (Landscape mode)
    }

  else if(IS_IPHONE)
    {

        if ([[UIScreen mainScreen] bounds].size.height == 568)
        {
            //Write code here for iPhone 5    (Landscape mode) 
        }
        else
        {
           //Write code here for iPhone(3.5 inch) (Landscape mode) 
        }
    }
}

-(void)setFrameForPortraitAndUpsideDownOrientation
{
    if(IS_IPAD)
    {

    }
    else if(IS_IPHONE)
    {

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

        }
        else
        {

        }
    }

}

答案 4 :(得分:0)

  #define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model] isEqualToString : @"iPhone     Simulator"])
  #define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : @"iPhone"])
  #define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : @"iPod Touch"])
  #define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
  #define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )



 if (ITS_IPHONE5)

 {

 }

 else

{

}

答案 5 :(得分:0)

在支持文件夹的project-Prefix.pch文件中使用此代码。

    #define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model] 
            isEqualToString : @"iPhone     Simulator"])
    #define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : @"iPhone"])
    #define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : @"iPod     Touch"])
    #define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
    #define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )

复制并过去此代码之后在您的.m文件中通过第一个代码我给你...   不需要在viewdidload上写大小只写在这些小床中

       if  (IS_IPAD)
            {

            }
          else if(IS_IPHONE)
             {

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

                }
         else
                {

                }
             }