前言
在百老师搜索啦很多关于按比例适配屏幕内容的方案, 但是一直没找到一个适合自己的项目的, 第一目前的项目是纯代码编写的而没有使用xib和storyboard, 第二希望找到一个可以适配全局的方案, 从而达到代码优化的目的.通过耐心的寻找,终于让我找到了一个适合当前项目的方案.
解决方案
使用遍历的方式进行当前控制器的视图的遍历,获取到视图然后在进行约束 字体 圆角等通过基准数值也就是我们开发的时候的原尺寸按比例缩放.
```
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, BSAdaptScreenWidthType) {
AdaptScreenWidthTypeNone = 0,
BSAdaptScreenWidthTypeConstraint = 1<<0, /**< 对约束的constant等比例 */
BSAdaptScreenWidthTypeFontSize = 1<<1, /**< 对字体等比例 */
BSAdaptScreenWidthTypeCornerRadius = 1<<2, /**< 对圆角等比例 */
BSAdaptScreenWidthTypeAll = 1<<3, /**< 对现有支持的属性等比例 */
};
@interface UIView (BSAdaptScreen)
/**
遍历当前view对象的subviews和constraints,对目标进行等比例换算
@param type 想要和基准屏幕等比例换算的属性类型
@param exceptViews 需要对哪些类进行例外
*/
- (void)adaptScreenWidthWithType:(BSAdaptScreenWidthType)type
exceptViews:(NSArray<Class> *)exceptViews;
@end
```
```
#import "UIView+AdaptScreen.h"
// 基准屏幕宽度
#define kRefereWidth1024.0
// 以屏幕宽度为固定比例关系,来计算对应的值。假设:基准屏幕宽度375,floatV=10;当前屏幕宽度为750时,那么返回的值为20
#define AdaptW(floatValue) (floatValue*[[UIScreen mainScreen] bounds].size.width/kRefereWidth)
@implementationUIView(AdaptScreen)
- (void)adaptScreenWidthWithType:(AdaptScreenWidthType)type
exceptViews:(NSArray *)exceptViews {
if (![self isExceptViewClassWithClassArray:exceptViews]) {
// 是否要对约束进行等比例
BOOL adaptConstraint = ((type & AdaptScreenWidthTypeConstraint) || type == AdaptScreenWidthTypeAll);
// 是否对字体大小进行等比例
BOOL adaptFontSize = ((type & AdaptScreenWidthTypeFontSize) || type == AdaptScreenWidthTypeAll);
// 是否对圆角大小进行等比例
BOOL adaptCornerRadius = ((type & AdaptScreenWidthTypeCornerRadius) || type == AdaptScreenWidthTypeAll);
// 约束
if(adaptConstraint) {
[self.constraintsenumerateObjectsUsingBlock:^(__kindofNSLayoutConstraint*_NonnullsubConstraint,NSUIntegeridx,BOOL*_Nonnullstop) {
subConstraint.constant=AdaptW(subConstraint.constant);
}];
}
// 字体大小
if(adaptFontSize) {
if ([self isKindOfClass:[UILabel class]] && ![self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
UILabel*labelSelf = (UILabel*)self;
labelSelf.font= [UIFontsystemFontOfSize:AdaptW(labelSelf.font.pointSize)];
}
elseif([selfisKindOfClass:[UITextFieldclass]]) {
UITextField*textFieldSelf = (UITextField*)self;
textFieldSelf.font= [UIFontsystemFontOfSize:AdaptW(textFieldSelf.font.pointSize)];
}
else if([selfisKindOfClass:[UIButtonclass]]) {
UIButton*buttonSelf = (UIButton*)self;
buttonSelf.titleLabel.font= [UIFontsystemFontOfSize:AdaptW(buttonSelf.titleLabel.font.pointSize)];
}
else if([selfisKindOfClass:[UITextViewclass]]) {
UITextView*textViewSelf = (UITextView*)self;
textViewSelf.font= [UIFontsystemFontOfSize:AdaptW(textViewSelf.font.pointSize)];
}
}
// 圆角
if(adaptCornerRadius) {
if(self.layer.cornerRadius) {
self.layer.cornerRadius=AdaptW(self.layer.cornerRadius);
}
}
[self.subviewsenumerateObjectsUsingBlock:^(__kindofUIView*_NonnullsubView,NSUIntegeridx,BOOL*_Nonnullstop) {
// 继续对子view操作
[subViewadaptScreenWidthWithType:typeexceptViews:exceptViews];
}];
}
}
// 当前view对象是否是例外的视图
- (BOOL)isExceptViewClassWithClassArray:(NSArray<Class> *)classArray {
__blockBOOLisExcept =NO;
[classArrayenumerateObjectsUsingBlock:^(Class _Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
if([selfisKindOfClass:obj]) {
isExcept =YES;
*stop =YES;
}
}];
returnisExcept;
}
```
总结
通过遍历达到适配的目的, 同时使用约束进行配合使用. 在项目使用过程中在进行完善, 感谢小生不怕提供的思路, 来自https://www.jianshu.com/p/cf049bebdc6c