- 注意:Swift中的”#pragma mark”
在今天初用Swift编码时,我发现平时很习惯的 #pragma mark 不见了,去查了资料才发现Swift中这个也改了,现在Swift中的用法如下
// MARK: - XXXXXX 相当于#pragma mark - XXXXXX
// TODO: XXXXXX 相当于#pragma mark XXXXXX
// FIXME: XXXXXX 相当于额....随意了
如果MARK后不加 - ,那么就不会有分割线效果
一、Swift宏命令:
import UIKit
// 自动布局,相当于 Objective-C 中的 Masonry,(Swift 中使用 SnapKit)
import SnapKit
// MARK: - 其他 -
/**
* 1、颜色
*/
// rgb颜色转换(16进制->10进制)
func RGBA(R:CGFloat,G:CGFloat,B:CGFloat,A:CGFloat) ->UIColor {
return UIColor(red: R/255.0, green: G/255.0, blue: B/255.0, alpha: A)
// return UIColor.init(red: R/255.0, green: G/255.0, blue: B/255.0, alpha: A)
}
//弱引用
//let weakSelf __weak __typeof(&*self)weakSelf = self;
// MARK: - 尺寸信息: -
/* 状态栏高度 20 或 44 */
// 然而从iOS 14开始,全面屏iPhone的状态栏高度不一定是 44 或 20 了,比如下面就是这些设备在iOS 14.1上的状态栏高度。(还可能时47、48)
/// ①、顶部状态栏高度(包括安全区)
func k_Height_statusBar() -> CGFloat {
var statusBarHeight: CGFloat = 0;
if #available(iOS 13.0, *) {
let scene = UIApplication.shared.connectedScenes.first;
guard let windowScene = scene as? UIWindowScene else {return 0};
guard let statusBarManager = windowScene.statusBarManager else {return 0};
statusBarHeight = statusBarManager.statusBarFrame.height;
} else {
statusBarHeight = UIApplication.shared.statusBarFrame.height;
}
return statusBarHeight;
}
/// ②、顶部安全区高度 k_Height_safeAreaInsetsTop
func k_Height_safeAreaInsetsTop() -> CGFloat {
if #available(iOS 13.0, *) {
let scene = UIApplication.shared.connectedScenes.first;
guard let windowScene = scene as? UIWindowScene else {return 0}; // guard:如果 expression 值计算为false,则执行代码块内的 guard 语句。(必须包含一个控制语句: return、 break、 continue 或 throw。)。as?:类型转换,(还有这两种:as、as!)
guard let window = windowScene.windows.first else {return 0};
return window.safeAreaInsets.top;
} else if #available(iOS 11.0, *) {
guard let window = UIApplication.shared.windows.first else {return 0};
return window.safeAreaInsets.top;
}
return 0;
}
/// ③、底部安全区高度
func k_Height_safeAreaInsetsBottom() -> CGFloat {
if #available(iOS 13.0, *) {
let scene = UIApplication.shared.connectedScenes.first;
guard let windowScene = scene as? UIWindowScene else {return 0};
guard let window = windowScene.windows.first else {return 0};
return window.safeAreaInsets.bottom;
} else if #available(iOS 11.0, *) {
guard let window = UIApplication.shared.windows.first else {return 0};
return window.safeAreaInsets.bottom;
}
return 0;
}
/** 屏幕宽度 */
let kScreenWidth = UIScreen.main.bounds.size.width
/** 屏幕高度 */
let kScreenHeight = UIScreen.main.bounds.size.height
/* 导航栏高度 固定高度 = 44.0f */
//let k_Height_NavContentBar :CGFloat = UINavigationBar.appearance().frame.size.height
let k_Height_NavContentBar :CGFloat = 44.0
/** 状态栏高度 */
let k_Height_StatusBar :CGFloat = k_Height_statusBar()
/** 状态栏+导航栏的高度 */
let k_Height_NavigationtBarAndStatuBar: CGFloat = k_Height_NavContentBar + k_Height_StatusBar
/** 底部tabBar栏高度(不包含安全区,即:在 iphoneX 之前的手机) */
let k_TabBar_Height :CGFloat = 49.0
/** 底部导航栏高度(包括安全区),一般使用这个值 */
let k_Height_TabBar :CGFloat = k_Height_safeAreaInsetsBottom() + k_TabBar_Height
// MARK: - 检查系统版本 -
/* 检查系统版本 */
/// 版本号相同:
func systemVersionEqual(version:String) -> Bool {
return UIDevice.current.systemVersion == version
}
/// 系统版本高于等于该version 测试发现只能传入带一位小数点的版本号 不然会报错 具体原因待探究
func systemVersionGreaterThan(version:String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric, range: version.startIndex..<version.endIndex, locale: Locale(identifier:version)) != ComparisonResult.orderedAscending
}
//判断是否是 x、及x以上 系列
func isIphoneX() -> Bool {
return k_Height_safeAreaInsetsBottom() > 0.0; // 底部安全区 > 0 时,
}
例如,在iphone11 运行,输入结果如下:
print(" 屏幕 宽度 = \(kScreenWidth) \n",
"屏幕 高度 = \(kScreenHeight) \n",
"导航栏 高度 = \(k_Height_NavContentBar) \n",
"状态栏 高度 = \(k_Height_StatusBar) \n",
"状态栏+导航栏的 高度 = \(k_Height_NavigationtBarAndStatuBar) \n",
"底部tabBar栏 高度 = \(k_TabBar_Height) \n",
"底部导航栏高度(包括安全区) = \(k_Height_TabBar) \n",
"该设备是否是 iphoneX以上系列的手机📱 = \(isIphoneX())"
)
/**
屏幕 宽度 = 414.0
屏幕 高度 = 896.0
导航栏 高度 = 44.0
状态栏 高度 = 48.0
状态栏+导航栏的 高度 = 92.0
底部tabBar栏 高度 = 49.0
底部导航栏高度(包括安全区) = 83.0
该设备是否是 iphoneX以上系列的手机📱 = true
*/
二、Objective-C宏命令:
1、问题:
有些情况下,我们需要获取屏幕的状态栏高度,很多人使用类似下面的宏:
#define StatusBar_HEIGHT (DEVICE_IS_FULL_DISPLAY ? 44.: 20.)
然而从iOS 14开始,全面屏iPhone的状态栏高度不一定是44了,比如下面就是这些设备在iOS 14.1上的状态栏高度。
设备 | 状态栏高度 |
---|---|
iPhone XR/11 | 48 |
iPhone X/11 Pro/ 11 Pro Max/12 mini | 44 |
iPhone 12/12 Pro/Pro Max | 47 |
解决办法:
根据不同系统,通过相应方法获取状态栏高度。使用下面的:k_Height_StatusBar
+ (CGFloat)statusBarHeight {
CGFloat statusBarHeight = 0;
if (@available(iOS 13.0, *)) {
statusBarHeight = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;
} else {
statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
}
return statusBarHeight;
}
在使用命令宏时,这样写:
//判断是否是 x系列
#define iPhoneX \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})
//NavBar高度
#define k_Height_NavContentBar 44.0f
//状态栏 statusBar 高度
//#define k_Height_StatusBar [[UIApplication sharedApplication] statusBarFrame].size.height
// 然而从iOS 14开始,全面屏iPhone的状态栏高度不一定是 44 或 20 了,比如下面就是这些设备在iOS 14.1上的状态栏高度。(还可能时47、48)
#define k_Height_StatusBar \
({\
CGFloat statusBarHeight = 0;\
if (@available(iOS 13.0, *)) {\
statusBarHeight =[UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;\
} else {\
statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;\
}\
(statusBarHeight);\
})
//导航栏的高度
#define k_Height_NavBar k_Height_StatusBar + 44
//TabBar高度
#define k_Height_TabBar (iPhoneX ? 83.0 : 49.0)
//底部安全区的高度
#define k_XBOTTOM_HEIGHT (iPhoneX ? 34.0 : 0.0)
在iOS 13.0之后, 获取keyWindow,比如使用下面代码使用时:[[self getKeyWindow] addSubView:subView];
/**
* 获取keyWindow
*/
+ (UIWindow *)getKeyWindow {
if (@available(iOS 13.0, *)) {
for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) {
if (windowScene.activationState == UISceneActivationStateForegroundActive) {
for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window;
}
}
}
}
} else {
return [UIApplication sharedApplication].keyWindow;
}
return nil;
}
3、下面是其他一下宏命令:
#define WEAK_SELF __weak typeof(self)weakSelf = self;
//#MARK: 设备信息判断
//ios11以上宏
#define iOS11 ([[[UIDevice currentDevice]systemVersion] floatValue] >= 11.0)
#define IOS10 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10)
#define IOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9)
#define IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
#define IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhonePlus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? ((CGSizeEqualToSize(CGSizeMake(750, 1624), [[UIScreen mainScreen] currentMode].size))||(CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size))||(CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size))||(CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size))) : NO)
//#MARK: 尺寸信息配置
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScaleW MIN(kScreenWidth,kScreenHeight) / 375.0
#define kScaleH MAX(kScreenWidth,kScreenHeight) / 667.0
#define KWScale(x) x*kScaleW
/// 比例尺寸 (取MIN 防止横屏比例尺变更)
#define kRealValueWidthIn6(w) ((MIN(kScreenWidth,kScreenHeight)/375.0)*(w))
/// 比例size
#define kRealValueSizeIn6(w,h) CGSizeMake(kRealValueWidthIn6(w), kRealValueWidthIn6(h))
#define kBottomSafty (iPhone_X ? (34):(0))
#define kStatusSafty (iPhone_X ? (22):(0))
// 状态栏高度
#define kStatusBarHeight (iPhone_X ? (44):(20))
// 状态栏+导航栏高度
#define kNavBarHeight (iPhone_X ? 88.f : 64.f)
// 底部标签栏高度
#define kTabBarHeight (iPhone_X ? (49.f+34.f) : 49.f)
#define HTDiscoverHeight (iPhone_X ? (49.f+34.f) : 0)
#define MSWidth iPhonePlus ? 412 : 372
#define phone_status_height [[UIApplication sharedApplication] statusBarFrame].size.height
#define phone_navBar_height self.navigationController.navigationBar.frame.size.height
//#MARK: 字体信息配置
#define FONT(F) [UIFont fontWithName:@"PingFangSC-Regular" size:F*kScaleW]
#define FONT_Medium(F) [UIFont fontWithName:@"PingFangSC-Medium" size:F*kScaleW]
#define FONTBOLD(F) [UIFont fontWithName:@"PingFangSC-Semibold" size:F*kScaleW]
#define HCBOLD(F) [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:F*kScaleW]
#define kFONT(F) [UIFont fontWithName:@"PingFangSC-Regular" size:F]
#define kFONT_Medium(F) [UIFont fontWithName:@"PingFangSC-Medium" size:F]
#define kFONT_SC(F) [UIFont fontWithName:@"PingFangSC" size:F*kScaleW]
//#MARK: 颜色信息配置
//#MARK: 背景颜色
/// 灰底
#define kGrayBackgroundColor hexColor(#F8F9FA)
#define kGrayLightBackgroundColor hexColor(#EEEEEE)
/// 白底
#define kWhiteBackgroundColor hexColor(#FFFFFF)
/// 透明
#define kClearBackgroundColor [UIColor clearColor]
/// 黄底
#define kYellowBackgroundColor hexColor(#F3B61F)
/// 蓝底
#define KBlueBackgroundColor hexColor(#A095FF)
/// 浅蓝底
#define KBlueLightBackgroundColor hexColor(#6F62F9)
//#MARK: 分割线颜色
/// 灰线
#define kGrayLineColor hexColor(#D9D9D9)
#define kGrayLightLineColor hexColor(#F4F4F4)
//#MARK: 字体颜色
/// 黑字
#define kBlackWordColor hexColor(#262626)
/// 灰字
#define kDarkGrayWordColor hexColor(#929292)
#define kGrayWordColor hexColor(#A8A8A8)
#define kGrayLightWordColor hexColor(#666666)
/// 白字
#define kWhiteWordColor hexColor(#FFFFFF)
/// 红字
#define kRedWordColor hexColor(#F40000)
/// 黄字
#define kYellowWordColor hexColor(#F3B61F)
/// 蓝字
#define KBlueWordColor hexColor(#6248FF)
网友评论