UIAdaptiveKit
-
LayoutTool.swift
: UI自動布局的便捷方法, 主要有常用的 寬度, 高度, 字體大小設(shè)置. 非常用的封裝在LayoutTool的struct里面. -
LayoutMethod.swift
: 為 LayoutTool提供實現(xiàn)的方法 -
UIDevice+Extension.swift
: 為設(shè)備提供便捷方法 例如判斷機型, 系統(tǒng)類型 等等.
相關(guān)代碼如下:
LayoutTool.swift
import UIKit
///適配手機和平板的寬度
public func autoWidth(_ width: CGFloat) -> CGFloat {
if UIApplication.shared.statusBarOrientation.isLandscape {
return LayoutMethod.autoLayoutWidth(iPhoneWidth: width)
}else {
return LayoutMethod.autoLayoutHeight(iPhoneHeight: width)
}
}
///適配手機和平板的高度
public func autoHeihgt(_ height: CGFloat) -> CGFloat {
if UIApplication.shared.statusBarOrientation.isLandscape {
return LayoutMethod.autoLayoutHeight(iPhoneHeight: height)
}else {
return LayoutMethod.autoLayoutWidth(iPhoneWidth: height)
}
}
///系統(tǒng)字號
func autoFontSize(_ font: Float) -> UIFont {
let floatSize = UIDevice.isIpad ? font * 1.5 : font
let font : UIFont = UIFont.systemFont(ofSize: CGFloat(floatSize))
return font
}
struct LayoutTool{
///加粗的系統(tǒng)字號
static func autoBoldfontSize(_ font: Float) -> UIFont {
let floatSize = UIDevice.isIpad ? font * 1.5 : font
let font : UIFont = UIFont.boldSystemFont(ofSize: CGFloat(floatSize))
return font
}
///安全距離的Insets
static var safeAreaInsets: UIEdgeInsets {
if #available(iOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets ?? .zero
}
return .zero
}
///左邊安全距離
static let leftSafeInset = safeAreaInsets.left
///右邊安全距離
static let rightSafeInset = safeAreaInsets.right
///上邊安全距離
static let topSafeInset = safeAreaInsets.top
///下邊安全距離
static let bottomSafeInset = safeAreaInsets.bottom
///橫屏下的屏幕寬度
static let autoScreenWidth = max(UIScreen.main.bounds.height, UIScreen.main.bounds.width)
///橫屏下的屏幕高度
static let autoScreenHeight = min(UIScreen.main.bounds.height, UIScreen.main.bounds.width)
}
LayoutMethod.swift
import UIKit
struct LayoutMethod {
///橫屏情況下的寬度設(shè)置
///
/// - 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系列
autoWidth = (iPhoneWidth * ((actualwidth - 78.0) / normalWidth)).rounded(3)//精確到小數(shù)點后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
}
///橫屏情況下的高度設(shè)置
///
/// - 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
}
}
public extension CGFloat {
///精確到小數(shù)點后幾位
func rounded(_ decimalPlaces: Int) -> CGFloat {
let divisor = pow(10.0, CGFloat.maximum(0, CGFloat(decimalPlaces)))
return CGFloat((CGFloat(self) * divisor).rounded() / divisor)
}
}
UIDevice+Extension.swift
import UIKit
extension UIDevice {
// MARK: - 判斷 機型
static let isIphone = UIDevice.current.userInterfaceIdiom == .phone
static let isIpad = UIDevice.current.userInterfaceIdiom == .pad
/// 判斷是否為劉海屏 iphonex系列
static func isiPhoneXSeries() -> Bool {
guard #available(iOS 11.0, *) else {
return false
}
return UIApplication.shared.windows[0].safeAreaInsets != UIEdgeInsets.zero
}
// MARK: - 系統(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
}
}