ios 纯代码怎么适配ipad_iOS开发中iPhone和iPad的布局适配(工具篇)
UIAdaptiveKit
image.png
LayoutTool.swift : UI自动布局的便捷方法, 主要有常用的 宽度, 高度, 字体大小设置. 非常用的封装在LayoutTool的struct里面.
LayoutMethod.swift : 为 LayoutTool提供实现的方法
UIDevice+Extension.swift : 为设备提供便捷方法 例如判断机型, 系统类型 等等.
相关代码如下:
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)
}
}
///系统字号
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{
///加粗的系统字号
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 {
///横屏情况下的宽度设置
///
/// - 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)//精确到小数点后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
}
///横屏情况下的高度设置
///
/// - 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 {
///精确到小数点后几位
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: - 系统类型
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
}
}
ios 纯代码怎么适配ipad_iOS开发中iPhone和iPad的布局适配(工具篇)相关推荐
- 移动端采用Flexible将PX转换REM适配及开发中Retina屏1px边框的两种解决方案
移动端采用Flexible将PX转换REM适配及开发中Retina屏1px边框的两种解决方案 说明:两个方案均基于Webpack构建. 方案一: 搭建环境及相关配置 webpack 3,需要loade ...
- PHP+MySql的网络验证源码开源纯代码可二次开发
PHP+MySql的网络验证源码开源纯代码可二次开发 :99415656994962582靓仔担当有决心
- 低代码/无代码平台在软件开发中的应用
随着技术的不断发展,软件开发也在不断地进步.低代码/无代码平台已经成为软件开发的一个新的趋势.在这篇文章中,我们将深入探讨低代码/无代码平台在软件开发中的应用,包括它们的优势.如何选择合适的平台以及如 ...
- java ee有哪些工具_JavaEE开发中最常用到的技术和工具汇总
原标题:JavaEE开发中最常用到的技术和工具汇总 今天千锋广州小编给大家来介绍一下关于目前JavaEE开发中最常用到的技术和工具的介绍,下面我们一起来看一下吧. 项目管理:Ant,项目管理事实上的标 ...
- 开发中遇到的问题和经验 记录 ------- 后端篇
核心价值就是把现实世界的业务操作搬到计算机上,通过计算机软件和网络进行业务和数据处理,但是时至今日,能用计算机软件提高效率的地方,几乎已经被全部发掘过了,必须能够发掘出用户自己都没有发现的需求,必须洞 ...
- 安卓开发中的占位符在布局XML中使用
安卓开发中的占位符在布局XML中使用 刚开始学Android,实现用户注册登录功能EditText使用占位符. == 普通的英文半角空格 == == == no-break space ( ...
- ios 纯代码怎么适配ipad_iOS屏幕适配(纯代码)
在iOS实际项目开发中, 我们经常要适配不同尺寸的屏幕,如iPhone4s,iPhone5/s,iPhone6/s,iPhone6Plus等. 在代码中创建一个控件如: UILabel *label ...
- iOS纯代码工程手动快速适配
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- ios 纯代码怎么适配ipad_iPad横竖屏下的代码适配
你可能非常了解用不同的方式去适配不同尺寸的iPhone屏幕,在适配iPhone屏幕时你需要考虑的只是屏幕大小变化带来的UI元素间隔的变化,但是在iPad上主要针对的是横竖屏下完全不同的UI元素的布局, ...
- iOS一行代码让你的应用中UIScrollView的滑动与侧滑返回并存
侧滑返回是iOS系统的一个很贴心的功能,特别是在大屏手机上,单手操作的时候去按左上角的返回键特别不方便.当我在使用一个APP的时候,如果控制器不能侧滑返回,我会觉得这个APP十分不友好...这款产品在 ...
最新文章
- directx修复工具win7_教你安装双系统,win7+win10
- hive的Specified key was too long; max key length is 767 bytes问题解决
- Mybatis中的@Param注解
- HZNU 2019 Summer training 8
- # 20172307 2018-2019-1 《程序设计与数据结构》第5周学习总结
- ubuntu memcached php,如何在 Ubuntu 18.04 上安装 Memcached
- 中奖福利事宜 和 跪求意见
- python进行usb通讯_Python实现树莓派USB串口通讯
- (22)css3新增边框圆角属性border-radius
- HDU1693 Eat The Trees(插头dp)
- 在html中使用css的主要方式有,html中使用css的方法有哪几种
- c++_String一些使用记录
- 图论算法真的那么难吗?知识点都在这了……
- 2022 面试必刷 461 道大厂架构面试真题汇总 + 面经 + 简历模板
- Web前端之仿携程首页布局
- java基础知识入门大全(十年经验总结)
- jquery实现滑动滚动条出现对联广告
- Dos窗口的打开与基本命令
- Win32DiskImager写img到u盘报“拒绝访问”解决办法
- Android开发——集成友盟社会化分享遇到的坑(一)