美文网首页程序员iOS收藏iOS 适配
xib简单适配(适配iPhone X)

xib简单适配(适配iPhone X)

作者: 吃货_X | 来源:发表于2018-07-05 21:51 被阅读19次

一句代码适配xib。
[XLBTools viewLayoutWithXib:view];
下面的是详细代码(可以直接用)。


/** 判断iPhoneX */
#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

/** 自适应宽高 */
#define ADAPTATION_WIDTH(Width) [UIScreen mainScreen].bounds.size.width * (Width) / 375.0
#define ADAPTATION_HEIGHT(Height) ([UIScreen mainScreen].bounds.size.height - isPhoneX) * (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 (iPhoneX) {
            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))];
        }
    }
}

xib原图


屏幕快照 2018-07-05 21.43.30.png

8效果图


屏幕快照 2018-07-05 21.45.54.png

8p效果图


屏幕快照 2018-07-05 21.44.07.png

iPhone X效果图


屏幕快照 2018-07-05 21.44.28.png

相关文章

网友评论

    本文标题:xib简单适配(适配iPhone X)

    本文链接:https://www.haomeiwen.com/subject/gdjmuftx.html