UIAutoAdaptKit
- 根据iOS设备屏幕尺寸比例计算调整
- 支持横竖屏屏幕适配
- 支持cocoapods :
pod 'UIAutoAdaptKit'
github链接 : https://github.com/Xianlau/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
}
}