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

DXDevice.h文件

#import 

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

你可能感兴趣的:(iOS判断屏幕尺寸的常用工具类)