简介
在做ios开发的过程中,总会遇到一些用约束无法解决适配问题的情况,这个时候我们要计算出位置,然后手动的用代码控制控件的适配问题。
适配规则类的实现
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, ScreenModel) {
ScreenModelInch35 = 0, //(320 * 480)
ScreenModelInch4,//(320 * 570)
ScreenModelBigger, //(375)
ScreenModelPlus //(414)
};
//首先写一个方法来判断目前屏幕的尺寸
static inline ScreenModel calScreenModel(){
NSInteger nativeWidth = [UIScreen mainScreen].bounds.size.width;
if (nativeWidth <= 320){
CGFloat nativeHeight = [UIScreen mainScreen].bounds.size.height;
if (nativeHeight > 480){
return ScreenModelInch4;
}
return ScreenModelInch35;
}
if (nativeWidth <= 375){
return ScreenModelBigger;
}
return ScreenModelPlus;
}
@interface Ruler : NSObject
@property (nonatomic, strong) id inch35; //3.5
@property (nonatomic, strong) id inch4; //4.0
@property (nonatomic, strong) id inchBigger;//4.7
@property (nonatomic, strong) id inchPlus; //5.5
@property (nonatomic, assign, readonly) id value;
@end
//外部运用的方法,四个参数分别是不同尺寸屏幕,手动想控制的值
static inline Ruler* rulerMake(id inch35,id inch4,id inchBigger,id inchPlus){
Ruler *ruler = [[Ruler alloc] init];
ruler.inch35 = inch35;
ruler.inch4 = inch4;
ruler.inchBigger = inchBigger;
ruler.inchPlus = inchPlus;
return ruler;
}
#import "Ruler.h"
@implementation Ruler
//在value的getter方法里给来判断要返回给页面要用的值
- (id)value{
ScreenModel screenModel = calScreenModel();
switch (screenModel) {
case ScreenModelInch35:
return self.inch35;
break;
case ScreenModelInch4:
return self.inch4;
break;
case ScreenModelBigger:
return self.inchBigger;
break;
case ScreenModelPlus:
return self.inchPlus;
break;
default:
break;
}
return nil;
}
@end
这样就可以根据不同的屏幕尺寸设置不同的位置了。
还有本文用到了内联函数,内联函数的用处就不多说了,贴个链接吧。
(inline)内联函数在IOS开发中的使用