iOS 适配宏 - 导航栏、启动图
2021-12-03 本文已影响0人
Mr_Zhou
最近开发遇到一些导航栏高度的问题,网上搜索了很多,自己写了一些宏,在此进行记录。
导航栏适配
//设置颜色
#define ColorA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
#define RGB(r, g, b) ColorA((r), (g), (b), 255)
#define KeyWindow [UIApplication sharedApplication].keyWindow
#define TOPWindow [[[UIApplication sharedApplication] windows] lastObject]
//通知
#define KNotification [NSNotificationCenter defaultCenter]
//打印
#ifdef DEBUG
#define SSLog(...) NSLog(__VA_ARGS__)
#else
#define SSLog(...)
#endif
//强弱引用
#define SSLWeakSelf(type) __weak typeof(type) weak##type = type;
#define SSLStrongSelf(type) __strong typeof(type) type = weak##type;
//屏幕宽高
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
// 自己搜索记录了一些机型
//iPhoneX / iPhoneXS / iPhone 11 Pro / iPhone 12 mini (156)
#define SSL_isIphoneX_XS (kWidth == 375.f && kHeight == 812.f ? YES : NO)
// iPhone 12 / iPhone 12 Pro / iPhone 13 / iPhone 13 Pro (163)
#define SSL_isIphone12_Pro (kWidth == 390.f && kHeight == 844.f ? YES : NO)
//iPhoneXR / iPhoneXSMax / iPhone 11 / iPhone 11 Pro Max (175)
#define SSL_isIphoneXR_XSMax (kWidth == 414.f && kHeight == 896.f ? YES : NO)
// iiPhone 12 Pro Max / iPhone 13 Pro Max (179)
#define SSL_isIphone12_Max (kWidth == 428.f && kHeight == 926.f ? YES : NO)
// 是否是刘海屏幕
#define SSL_isFullScreen \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})
// 电池栏高度
#define SSL_StatusBarHeight (SSL_isFullScreen ? 44.f : 20.f)
// 导航栏高度
#define SSL_NavigationBarHeight \
({CGFloat statusBarHeight = 0.0;\
if (@available(iOS 13.0, *)) {\
statusBarHeight = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;\
} else { \
statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;\
}\
(statusBarHeight);\
})
// tabBar高度
#define SSL_TabbarHeight (SSL_isFullScreen ? (49.f+34.f) : 49.f)
// 导航栏+电池栏高度.
#define SSL_StatusBarAndNavigationBarHeight (SSL_isFullScreen ? 88.f : 64.f)
// 安全区域高度
#define SSL_TabbarSafeBottomMargin (SSL_isFullScreen ? 34.f : 10.f)
//toolbar高度
#define SSL_ToolBarHeight (SSL_isFullScreen ? (44.f+SSL_TabbarSafeBottomMargin) : 44.f)
//安全区域
#define SSL_ViewSafeAreInsets(view) ({UIEdgeInsets insets; if(@available(iOS 11.0, *)) {insets = view.safeAreaInsets;} else {insets = UIEdgeInsetsZero;} insets;}
通过LaunchScreen.StroyBoard自定义启动图
苹果2020年4月以后,所有app必须使用LaunchScreen设置启动图
老项目必须先进行删除,在项目中找
Build Settings -> Asset Catalog Launch Image Set Name
新项目直接在 LaunchScreen.storyboard 中进行设置,如下设置
设置ViewController里面添加的背景图记得上下到superView,左右到 Safe Area
布局 view设置