2015-01-15 139 views
2

我的设计具有不同的偏移量和每种设备的尺寸。 有没有什么方法可以为故事板中的约束设置不同的值(使用尺寸类或其他),以便纵向使用不同的iPhone(它们都是紧凑的)。区分iphone 4 vs 5 vs 6 vs 6+人像尺寸

如果否 - 解决此类任务的最佳方法是什么?

UPDATE

例如,我有标志,在每个(包括不同的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]; 

我也不想为每个平台创建单独的故事板 - 它甚至更糟。

+0

其他那么这两个提议的解决方案,也许你可以创建一个偏移标志(几种不同图片),并通过代码与设备名称分配徽标(这是丑陋的地狱,如果你问我)。第四种解决方案可能是:不可能的。 – Miknash

回答

0

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

这可能看起来像: - 在.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)。你能再解释一下你想做什么吗?

-1

粘贴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]; 
     } 
    } 
} 
+0

这与约束无关,因此答案与问题无关 –

0

如果你做到了,你编程测试可以对屏幕高度(或宽度),例如:

//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