美文网首页界面工具IOS进阶
iOS判断屏幕尺寸的常用工具类

iOS判断屏幕尺寸的常用工具类

作者: 船长_ | 来源:发表于2016-02-18 13:15 被阅读565次

DXDevice.h文件

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, DeviceSize){
    UnknownSize = 0,
    iPhone35inch = 1,
    iPhone4inch = 2,
    iPhone47inch = 3,
    iPhone55inch = 4
};

@interface DXDevice : NSObject
+(DeviceSize)deviceSize;
@end

DXDevice.m文件

+(DeviceSize)deviceSize {
    
    CGFloat screenHeight=0;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    
    if (orientation ==  UIDeviceOrientationPortrait)
        screenHeight = [[UIScreen mainScreen] bounds].size.height;
    
    else if((orientation == UIDeviceOrientationLandscapeRight) || (orientation == UIInterfaceOrientationLandscapeLeft))
        screenHeight = [[UIScreen mainScreen] bounds].size.width;
    
    if (screenHeight == 480)
        return iPhone35inch;
    else if(screenHeight == 568)
        return iPhone4inch;
    else if(screenHeight == 667)
        return  iPhone47inch;
    else if(screenHeight == 736)
        return iPhone55inch;
    else
        return UnknownSize;
}

简单用法示例

- (void)awakeFromNib
{
    // 如果是4英寸,设置约束为60
    if ([DXDevice deviceSize] == iPhone4inch) {
        self.topConstraint.constant = 60;
    }
     // 如果是4.7英寸,设置约束为90
    if ([DXDevice deviceSize] == iPhone47inch) {
        self.topConstraint.constant = 90;
    }
}

相关文章

网友评论

  • 小蜜蜂Bee:FTFDevice这个是什么?什么调用deviceSize方法?
    船长_:@小蜜蜂Bee 手误,应该是DXDevice
  • dd7dc4b4fe01:可以请问下最后设置的这个约束有什么用吗,我现在刚学习用masonry做界面,不会怎么给不同机型做适配。
    船长_:@fan834377614 不同屏幕设置不同的约束,比如6p屏幕比较大,那么可以设置两个控件之间间距大点,屏幕小的可以设置间距小点

本文标题:iOS判断屏幕尺寸的常用工具类

本文链接:https://www.haomeiwen.com/subject/icvmkttx.html