一個(gè)能適配所有iphone和ipad屏幕適配工具UIAutoAdaptKit

UIAutoAdaptKit

一個(gè)能適配所有iphone和ipad的UI布局工具

  • 結(jié)合 SnapKit 或者 frame 布局使用, 就能一套UI代碼適配所有iOS設(shè)備.

       - autoWidth() : 兩個(gè)view之間的寬度距離
       - autoHeight() : 兩個(gè)view件之間的高度距離
       - autoLength() : 單個(gè)view之間的邊長(zhǎng) (單個(gè)view的寬和高都必須使用這個(gè),會(huì)自動(dòng)適配)
    
  • 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布局自動(dòng)適配
 *
 */


///設(shè)置單個(gè)控件的寬度或者高度 如一個(gè)button的寬度高度都需要用此方法
public func autoLength(_ lenght: CGFloat) -> CGFloat {
    if UIDevice.isIphone {
        return autoHeihgt(lenght)
    }else {
        return autoWidth(lenght)
    }
}

///設(shè)置兩個(gè)控件之間的左右距離 如左右兩個(gè)button的左右距離
public func autoWidth(_ width: CGFloat) -> CGFloat {
    if  UIApplication.shared.statusBarOrientation.isLandscape {
        return LayoutMethod.autoLayoutWidth(iPhoneWidth: width)
    }else {
        return  LayoutMethod.autoLayoutHeight(iPhoneHeight: width)
    }
}
///設(shè)置兩個(gè)控件之間的上下距離 如左右兩個(gè)button的相對(duì)上下距離
public func autoHeihgt(_ height: CGFloat) -> CGFloat {
    
    if  UIApplication.shared.statusBarOrientation.isLandscape {
        return LayoutMethod.autoLayoutHeight(iPhoneHeight: height)
    }else {
        return  LayoutMethod.autoLayoutWidth(iPhoneWidth: height)
    }
}

///設(shè)置系統(tǒng)字號(hào) 能自動(dòng)適配所有設(shè)備
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{
    
    ///加粗的系統(tǒng)字號(hào)
    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
    
    ///最長(zhǎng)的屏幕邊: 橫屏下的屏幕寬度 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 {

    ///最長(zhǎng)的屏幕邊距離設(shè)置: 橫屏下的屏幕寬度距離 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為標(biāo)準(zhǔn)  375 * 667
        let actualwidth = LayoutTool.autoScreenWidth//橫屏下的屏幕寬度
        //iphone的自動(dòng)布局
        if UIDevice.isIphone {
            if UIDevice.isiPhoneXSeries() {//是否iPhone X系列 減去 34 + 44
                autoWidth = (iPhoneWidth * ((actualwidth - 78.0) / normalWidth)).rounded(3)//精確到小數(shù)點(diǎn)后3位
            }else{
                 autoWidth = (iPhoneWidth * (actualwidth/normalWidth)).rounded(3)
            }
        //iPad的自動(dòng)布局
        }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
    }
    
     ///最短的屏幕邊距離設(shè)置: 橫屏下的屏幕高度距離 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為標(biāo)準(zhǔn)  375 * 667
        let actualHeight = LayoutTool.autoScreenHeight //橫屏下的屏幕高度
         //iphone的自動(dòng)布局
         if UIDevice.isIphone {
            autoHeight = (iPhoneHeight * (actualHeight/normalHeight)).rounded(3)
        //iPad的自動(dòng)布局
         }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 {
    ///精確到小數(shù)點(diǎn)后幾位
    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: - 判斷 機(jī)型
    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系統(tǒng)
    public class func isiOS14() -> Bool {
        if #available(iOS 14.0, *) {
            return true
        } else {
            return false
        }
    }
    ///是否iOS13系統(tǒng)
    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
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文我將為各位同學(xué)提到 屏幕適配的發(fā)展歷史 什么是適配? 什么是Autolayout? 使用代碼實(shí)現(xiàn)Autolay...
    iOS_ZZ閱讀 1,015評(píng)論 0 7
  • 久違的晴天,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí),離放學(xué)已經(jīng)沒(méi)多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,825評(píng)論 16 22
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開(kāi)了第一次的黨會(huì),身份的轉(zhuǎn)變要...
    余生動(dòng)聽(tīng)閱讀 10,871評(píng)論 0 11
  • 可愛(ài)進(jìn)取,孤獨(dú)成精。努力飛翔,天堂翱翔。戰(zhàn)爭(zhēng)美好,孤獨(dú)進(jìn)取。膽大飛翔,成就輝煌。努力進(jìn)取,遙望,和諧家園??蓯?ài)游走...
    趙原野閱讀 3,509評(píng)論 1 1
  • 在妖界我有個(gè)名頭叫胡百曉,無(wú)論是何事,只要找到胡百曉即可有解決的辦法。因?yàn)槭侵缓偞蠹乙杂瀭饔灲形摇皟A城百曉”,...
    貓九0110閱讀 3,715評(píng)論 7 3

友情鏈接更多精彩內(nèi)容