xib适配(适配iPhone X 系列)—— 之前写在简书

/** 自适应宽高 */
#define ADAPTATION_WIDTH(Width) [UIScreen mainScreen].bounds.size.width * (Width) / 375.0
#define ADAPTATION_HEIGHT(Height) [UIScreen mainScreen].bounds.size.height  * (Height) / 667.0

#pragma mark - XIB适配
+ (void)viewLayoutWithXib:(UIView *)viewLayout{
    //视图自适应布局
    for (UIView *view in viewLayout.subviews) {
        CGRect returnRect;
        returnRect.size.width = ADAPTATION_WIDTH(view.frame.size.width);
        returnRect.size.height = ADAPTATION_HEIGHT(view.frame.size.height);
        returnRect.origin.x = ADAPTATION_WIDTH(view.frame.origin.x);
        returnRect.origin.y = ADAPTATION_HEIGHT(view.frame.origin.y);
            if (view.frame.size.width == view.frame.size.height) {
                returnRect.size.width = ADAPTATION_HEIGHT(view.frame.size.width);
                returnRect.size.height = ADAPTATION_HEIGHT(view.frame.size.width);
            }
            if ([view isKindOfClass:[UIImageView class]] && view.frame.size.width == view.frame.size.height) {
                returnRect.origin.y = ADAPTATION_HEIGHT(view.frame.origin.y) + (ADAPTATION_HEIGHT(view.frame.size.height) - ADAPTATION_WIDTH(view.frame.size.height)) / 2.0;
                returnRect.size.width = ADAPTATION_WIDTH(view.frame.size.width);
                returnRect.size.height = ADAPTATION_WIDTH(view.frame.size.width);
            }
        view.frame = returnRect;
// 字体适配
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *viewField = (UITextField *)view;
            CGFloat fontSize = viewField.font.pointSize;
            NSString *fontName = viewField.font.fontName;
            viewField.font = [UIFont fontWithName:fontName  size:(ADAPTATION_WIDTH(fontSize))];
            
        }else if ([view isKindOfClass:[UILabel class]]){
            UILabel *viewLabel = (UILabel *)view;
            CGFloat fontSize = viewLabel.font.pointSize;
            NSString *fontName = viewLabel.font.fontName;
            viewLabel.font = [UIFont fontWithName:fontName  size:(ADAPTATION_WIDTH(fontSize))];
        }else if ([view isKindOfClass:[UIButton class]]){
            UIButton *viewButton = (UIButton *)view;
            CGFloat fontSize = viewButton.titleLabel.font.pointSize;
            NSString *fontName = viewButton.titleLabel.font.fontName;
            viewButton.titleLabel.font = [UIFont fontWithName:fontName  size:(ADAPTATION_WIDTH(fontSize))];
        }
    }
}

详情 https://www.jianshu.com/p/a8eb3bbdc00f

你可能感兴趣的:(xib适配(适配iPhone X 系列)—— 之前写在简书)