本文主要介绍两个方面的内容:1. 如何在swift中创建一个作用类似oc中的pch的文件。 2. swift中适配iPhone X
1. 如何在swift中创建一个作用类似oc中的pch的文件。
(1)新建一个名为Common的文件:File->New->File... 选择Swift File
![](https://img.haomeiwen.com/i12092998/57fac3c9bf3c5d9c.png)
(2)打开Common.swift文件,将 import Foundation 改为 import UIKit
![](https://img.haomeiwen.com/i12092998/5f35a445e64ea28a.png)
(3)加入全局变量,在其他类中调用即可
![](https://img.haomeiwen.com/i12092998/38a15fe27104b3cf.png)
2. swift中适配iPhone X
iPhone的全面屏手机和非全面屏手机的区别是: 状态栏的高度不同以及底部的安全距离不同。
全面屏手机的状态栏高度为44pt,非全面屏手机的状态栏高度为20pt; 全面屏手机的底部安全距离是34pt,非全面屏手机为0pt
(1)先获取状态栏的高度,根据状态栏的高度定义 导航栏高度、tabbar高度、顶部的安全距离、底部的安全距离
//适配iPhoneX
//获取状态栏的高度,全面屏手机的状态栏高度为44pt,非全面屏手机的状态栏高度为20pt
//状态栏高度
let statusBarHeight = UIApplication.shared.statusBarFrame.height;
//导航栏高度
let navigationHeight = (statusBarHeight + 44)
//tabbar高度
let tabBarHeight = (statusBarHeight==44 ? 83 : 49)
//顶部的安全距离
let topSafeAreaHeight = (statusBarHeight - 20)
//底部的安全距离
let bottomSafeAreaHeight = (tabBarHeight - 49)
(2)先获取底部的安全距离,根据底部的安全距离定义导航栏高度、tabbar高度、顶部的安全距离、状态栏高度
////适配iPhoneX
//获取底部的安全距离,全面屏手机为34pt,非全面屏手机为0pt
//底部的安全距离
let bottomSafeAreaHeight = UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
//顶部的安全距离
let topSafeAreaHeight = (bottomSafeAreaHeight == 0 ? 0 : 24)
//状态栏高度
let statusBarHeight = UIApplication.shared.statusBarFrame.height;
//导航栏高度
let navigationHeight = (bottomSafeAreaHeight == 0 ? 64 :88)
//tabbar 高度
let tabBarHeight = (bottomSafeAreaHeight + 49)
参考链接
https://www.cnblogs.com/hero11223/p/7363343.html
https://blog.csdn.net/qq_36487644/article/details/83016860
网友评论