ios 简单的使用按比例自适应屏幕

            //1.首先创建一个类,继承于NSObject,在.m中导入AVKit.AVKit.h框架

               

+ (CGRect)getNewRectWithRect:(CGRect)rect isHeight:(BOOL)isHeight{
    
    CGRect newRect = CGRectZero;
    newRect.origin = [self getNewPointWithPoint:rect.origin];
    newRect.size = [self getNewSizeWithSize:rect.size isHeight:isHeight];
    return newRect;
}
+ (CGPoint)getNewPointWithPoint:(CGPoint)point{

    CGPoint newpoint = CGPointZero;
    newpoint.x = point.x * W_Adapter;
    newpoint.y = point.y * H_Adapter;
    return newpoint;
}
+ (CGSize)getNewSizeWithSize:(CGSize)size isHeight:(BOOL)isHeight{
    CGSize newSize =  CGSizeZero;
    newSize.width = size.width * W_Adapter;
    if (isHeight) {
        newSize.height = size.height * H_Adapter;
    }else{
        newSize.height = size.height * W_Adapter;
    }
    return newSize;
}

     //2.在.h中写4个宏定义,然后将方法声明,让外部可以调用

    

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#define W_Adapter SCREEN_W/375.0
#define H_Adapter SCREEN_H/667.0

          //传入的Bool值是用于判断你是否需要用高度自适应

+ (CGRect)getNewRectWithRect:(CGRect)rect isHeight:(BOOL)isHeight;
+ (CGPoint)getNewPointWithPoint:(CGPoint)point;
+ (CGSize)getNewSizeWithSize:(CGSize)size isHeight:(BOOL)isHeight;


     //3.然后就是使用了,在你初始化控件的地方

UILabel *label = [[UILabel alloc] initWithFrame:[TextOfThe getNewRectWithRect:CGRectMake(0, 270, 115, 30) isHeight:YES]];



      

你可能感兴趣的:(ios)