一个能适配所有iphone和ipad屏幕适配工具UIAutoAdaptKit

UIAutoAdaptKit

一个能适配所有iphone和ipad的UI布局工具

  • 结合 SnapKit 或者 frame 布局使用, 就能一套UI代码适配所有iOS设备.

       - autoWidth() : 两个view之间的宽度距离
       - autoHeight() : 两个view件之间的高度距离
       - autoLength() : 单个view之间的边长 (单个view的宽和高都必须使用这个,会自动适配)
    
  • frame 布局:

        testView.frame = CGRect.init(x: autoWidth(20), y: autoHeihgt(20), width: autoLength(100), height: autoLength(100))
  • SnapKit 布局:
        testButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(-autoheight(50))
            make.centerX.equalToSuperview()
            make.width.equalTo(autoLength(100))
            make.height.equalTo(autoLength(30))
        }

具体代码逻辑

import UIKit

/*
 *UI布局自动适配
 *
 */


///设置单个控件的宽度或者高度 如一个button的宽度高度都需要用此方法
public func autoLength(_ lenght: CGFloat) -> CGFloat {
    if UIDevice.isIphone {
        return autoHeihgt(lenght)
    }else {
        return autoWidth(lenght)
    }
}

///设置两个控件之间的左右距离 如左右两个button的左右距离
public func autoWidth(_ width: CGFloat) -> CGFloat {
    if  UIApplication.shared.statusBarOrientation.isLandscape {
        return LayoutMethod.autoLayoutWidth(iPhoneWidth: width)
    }else {
        return  LayoutMethod.autoLayoutHeight(iPhoneHeight: width)
    }
}
///设置两个控件之间的上下距离 如左右两个button的相对上下距离
public func autoHeihgt(_ height: CGFloat) -> CGFloat {
    
    if  UIApplication.shared.statusBarOrientation.isLandscape {
        return LayoutMethod.autoLayoutHeight(iPhoneHeight: height)
    }else {
        return  LayoutMethod.autoLayoutWidth(iPhoneWidth: height)
    }
}

///设置系统字号 能自动适配所有设备
public func autoFontSize(_ font: Float) -> UIFont {

    var floatSize = UIDevice.isIpad ? font * 1.5 : font
    if UIDevice.isiPhone5() {
        if font > 20 {
            floatSize = font - 5
        }else if font > 15 {
            floatSize = font - 3
        }else {
            floatSize = font - 2
        }
        
    }
    let font : UIFont = UIFont.systemFont(ofSize: CGFloat(floatSize))
    return font
}

public struct LayoutTool{
    
    ///加粗的系统字号
    public static func autoBoldfontSize(_ font: Float) -> UIFont {

        var floatSize = UIDevice.isIpad ? font * 1.5 : font
        if UIDevice.isiPhone5() {
            if font > 20 {
                floatSize = font - 5
            }else if font > 15 {
                floatSize = font - 3
            }else {
                floatSize = font - 2
            }
            
        }
        let font : UIFont = UIFont.boldSystemFont(ofSize: CGFloat(floatSize))
        return font
    }

    ///安全距离的Insets
    public static var safeAreaInsets: UIEdgeInsets {
        if #available(iOS 11.0, *) {
            return UIApplication.shared.delegate?.window??.safeAreaInsets ?? .zero
        }
        return .zero
    }
    ///左边安全距离
    public static let leftSafeInset = safeAreaInsets.left
    ///右边安全距离
    public static let rightSafeInset = safeAreaInsets.right
    ///上边安全距离
    public static let topSafeInset = safeAreaInsets.top
    ///下边安全距离
    public static let bottomSafeInset = safeAreaInsets.bottom
    
    ///最长的屏幕边: 横屏下的屏幕宽度 or 竖屏下的高度
    public static let autoScreenWidth = max(UIScreen.main.bounds.height, UIScreen.main.bounds.width)
    
    ///最短的屏幕边: 横屏下的屏幕高度 or 竖屏下的宽度
    public static let autoScreenHeight = min(UIScreen.main.bounds.height, UIScreen.main.bounds.width)
}


internal struct LayoutMethod {

    ///最长的屏幕边距离设置: 横屏下的屏幕宽度距离 or 竖屏下的高度距离
    ///
    /// - Parameters:
    ///   - iPhoneWidth: iPhone6 垂直方向@2x尺寸
    ///   - iPadWidth: 分辨率比例为768*1024的iPad
    /// - Returns: 适配后的尺寸

    static  func autoLayoutWidth(iPhoneWidth: CGFloat, iPadWidth: CGFloat? = nil) -> CGFloat {
        var autoWidth: CGFloat = 0.0
        let normalWidth:CGFloat = 667.0//以iphone6为标准  375 * 667
        let actualwidth = LayoutTool.autoScreenWidth//横屏下的屏幕宽度
        //iphone的自动布局
        if UIDevice.isIphone {
            if UIDevice.isiPhoneXSeries() {//是否iPhone X系列 减去 34 + 44
                autoWidth = (iPhoneWidth * ((actualwidth - 78.0) / normalWidth)).rounded(3)//精确到小数点后3位
            }else{
                 autoWidth = (iPhoneWidth * (actualwidth/normalWidth)).rounded(3)
            }
        //iPad的自动布局
        }else if UIDevice.isIpad{
            
            guard let ipadW = iPadWidth else {
                autoWidth = (iPhoneWidth * (actualwidth/normalWidth)).rounded(3)
                return autoWidth
            }
            autoWidth = (ipadW * (actualwidth/normalWidth)).rounded(3)
        }
        return autoWidth
    }
    
     ///最短的屏幕边距离设置: 横屏下的屏幕高度距离 or 竖屏下的宽度距离
     ///
     /// - Parameters:
     ///   - iPhoneH: iPhone6 垂直方向
     ///   - iPadH: 分辨率比例为768*1024的iPad
     /// - Returns: 适配后的尺寸

    static func autoLayoutHeight(iPhoneHeight: CGFloat, iPadHeight: CGFloat? = nil) -> CGFloat {

        var autoHeight: CGFloat = 0.0
        let normalHeight:CGFloat = 375.0//以iphone6为标准  375 * 667
        let actualHeight = LayoutTool.autoScreenHeight //横屏下的屏幕高度
         //iphone的自动布局
         if UIDevice.isIphone {
            autoHeight = (iPhoneHeight * (actualHeight/normalHeight)).rounded(3)
        //iPad的自动布局
         }else if UIDevice.isIpad{
            
            guard let ipadH = iPadHeight else {
                autoHeight = (iPhoneHeight * (actualHeight/normalHeight)).rounded(3)
                return autoHeight
            }
            autoHeight = (ipadH * (actualHeight/normalHeight)).rounded(3)
         }
         return autoHeight
     }

}

internal extension CGFloat {
    ///精确到小数点后几位
    func rounded(_ decimalPlaces: Int) -> CGFloat {
        let divisor = pow(10.0, CGFloat.maximum(0, CGFloat(decimalPlaces)))
        return CGFloat((CGFloat(self) * divisor).rounded() / divisor)
    }
}

extension UIDevice {
    
    // MARK: - 判断 机型
    public static let isIphone = UIDevice.current.userInterfaceIdiom == .phone
    public static let isIpad = UIDevice.current.userInterfaceIdiom == .pad
    
    /// 判断是否为iphonex系列 有刘海屏
    public static func isiPhoneXSeries() -> Bool {
        
        guard #available(iOS 11.0, *) else {
            return false
        }
        return UIApplication.shared.windows[0].safeAreaInsets != UIEdgeInsets.zero
    }
    
    ///是否iOS14系统
    public class func isiOS14() -> Bool {
        if #available(iOS 14.0, *) {
            return true
        } else {
            return false
        }
    }
    ///是否iOS13系统
    public class func isiOS13() -> Bool {
        if #available(iOS 13.0, *) {
            return true
        } else {
            return false
        }
    }
    
    public class func isiOS12() -> Bool {
        if #available(iOS 12.0, *) {
            return true
        } else {
            return false
        }
    }
    
    public class func isiOS11() -> Bool {
        if #available(iOS 11.0, *) {
            return true
        } else {
            return false
        }
    }
    
    public class func isiOS10() -> Bool {
        if #available(iOS 10.0, *) {
            return true
        } else {
            return false
        }
    }
    
    public class func isiOS9() -> Bool {
        if #available(iOS 9.0, *) {
            return true
        } else {
            return false
        }
    }
    
    // MARK: - 屏幕类型
    @objc public class func isiPhoneX() -> Bool {
        if (UIScreen.main.currentMode?.size.equalTo(CGSize.init(width: 1125, height: 2436)))! {
            return true
        }
        return false
    }
    
    public class func isiPhone6PlusBigMode() -> Bool {
        if (UIScreen.main.currentMode?.size.equalTo(CGSize.init(width: 1125, height: 2001)))! {
            return true
        }
        return false
    }
    
    public class func isiPhone6Plus() -> Bool {
        if (UIScreen.main.currentMode?.size.equalTo(CGSize.init(width:1242, height: 2208)))! {
            return true
        }
        return false
    }
    
    public class func isiPhone6BigMode() -> Bool{
        if (UIScreen.main.currentMode?.size.equalTo(CGSize.init(width: 320, height: 568)))! {
            return true
        }
        return false
    }
    
    public class func isiPhone6() -> Bool {
        if (UIScreen.main.currentMode?.size.equalTo(CGSize.init(width:750, height: 1334)))! {
            return true
        }
        return false
    }
    
    public class func isiPhone5() -> Bool {
        if (UIScreen.main.currentMode?.size.equalTo(CGSize.init(width: 640, height: 1136)))! {
            return true
        }
        return false
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容

  • 本文我将为各位同学提到 屏幕适配的发展历史 什么是适配? 什么是Autolayout? 使用代码实现Autolay...
    iOS_ZZ阅读 872评论 0 7
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,401评论 16 21
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,498评论 0 11
  • 可爱进取,孤独成精。努力飞翔,天堂翱翔。战争美好,孤独进取。胆大飞翔,成就辉煌。努力进取,遥望,和谐家园。可爱游走...
    赵原野阅读 2,681评论 1 1
  • 在妖界我有个名头叫胡百晓,无论是何事,只要找到胡百晓即可有解决的办法。因为是只狐狸大家以讹传讹叫我“倾城百晓”,...
    猫九0110阅读 3,227评论 7 3