繁体   English   中英

通过尺寸分类区分iPhone 4 vs 5 vs 6 vs 6+人像

[英]Distinguish iphone 4 vs 5 vs 6 vs 6+ portrait with size class

我为每种设备设计了具有不同偏移量和元素大小的设计。 有什么方法可以为不同肖像的iphone(它们都是紧凑的)在情节提要中为约束设置不同的值(使用尺寸类或其他)?

如果没有,那么解决此任务的最佳方法是什么?

更新

例如,我有徽标,在每个 (包括不同的iPhone)平台上,徽标的顶部偏移都不同(甚至以磅为单位)。

我想避免这样的代码

- (CGFloat)topLogoConstraintAccordingToSize:(CGSize)size {
    CGFloat top = 0;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        if (size.height > size.width){
            top = 56;
        } else {
            top = 35;
        }
    } else {
        if (IS_IPHONE_4){
            top = 36;
        } else if (IS_IPHONE_5){
            top = 22;
        } else if (IS_IPHONE_6){
            top = 50;
        } else if (IS_IPHONE_6_PLUS){
            top = 56;
        }
    }
    return top;
}

//宇宙中的其他地方

self.logoTopConstraint.constant = [self topLogoConstraintAccordingToSize:size];

另外,我不想为每个平台创建单独的故事板-更糟糕的是。

您可以将约束与后面的代码连接起来,并根据需要更改值。

可能看起来像:-在.h文件中:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraint;

-in .m文件:

if ([platform isEqualToString:@"iPhone3,3"])    constraint.constant=1;//put value you want instead of 1,2 and 3
if ([platform isEqualToString:@"iPhone4,1"])    constraint.constant=1;
if ([platform isEqualToString:@"iPhone5,1"])    constraint.constant=2;
if ([platform isEqualToString:@"iPhone5,2"])    constraint.constant=2;
if ([platform isEqualToString:@"iPhone5,3"])    constraint.constant=2;
if ([platform isEqualToString:@"iPhone5,4"])    constraint.constant=2;
if ([platform isEqualToString:@"iPhone6,1"])    constraint.constant=2;
if ([platform isEqualToString:@"iPhone6,2"])    constraint.constant=2;
if ([platform isEqualToString:@"iPhone7,1"])    constraint.constant=3;
if ([platform isEqualToString:@"iPhone7,2"])    constraint.constant=3;

但是,我不确定您要达到什么目标。 每个元素都以磅为单位,并且看起来应该很好(1x,2x和3x)。 您能再解释一下您想做什么吗?

如果您以编程方式执行此操作,则可以针对屏幕的高度(或宽度)进行测试,例如:

//iphone 5
if UIScreen.main.bounds.size.height == 568.0 {
// your code here
}

iphone屏幕高度如下:

iPhone 5 = 568.0

iPhone 6 / 6s / 7 = 667.0

iPhone 6/7加= 736.0

将此行粘贴到标题下面的AppDelegate.m中

 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)
 #define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

并在应用程序中启动了

       UIStoryboard *mainStoryboard = nil;
      if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
   {


    if (IS_WIDESCREEN)
    {

        //4 inch screen
        mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];


    }
    else
    {

        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
        {
            mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];

        }
        else
        {
            //3.5 inch screen
            mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_legacy_IPhone" bundle:nil];
        }
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM