iOS-适配全面屏的Navigationbar和Tabbar高度
2019-12-19 本文已影响0人
Simple_Code
1.通过UIWindow分类获取
.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIWindow (SPSafeArea)
- (UIEdgeInsets)sp_layoutInsets;
/// 安全区域NavigationHeight(只有StatusBarHeight)
- (CGFloat)sp_safeAreaNavigationHeight;
/// navigationHeight
- (CGFloat)sp_navigationHeight;
/// 安全区域tabbarHeight (只有安全区域高度)
- (CGFloat)sp_safeAreaTabbarHeight;
/// tabbarHeight
- (CGFloat)sp_tabbarHeight;
@end
NS_ASSUME_NONNULL_END
.m
#import "UIWindow+SPSafeArea.h"
@implementation UIWindow (SPSafeArea)
- (UIEdgeInsets)sp_layoutInsets {
if (@available(iOS 11.0, *)) {
UIEdgeInsets safeAreaInsets = self.safeAreaInsets;
if (safeAreaInsets.bottom > 0) {
//参考文章:https://mp.weixin.qq.com/s/Ik2zBox3_w0jwfVuQUJAUw
return safeAreaInsets;
}
return UIEdgeInsetsMake(20, 0, 0, 0);
}
return UIEdgeInsetsMake(20, 0, 0, 0);
}
- (CGFloat)sp_safeAreaNavigationHeight {
return [self sp_layoutInsets].top;
}
- (CGFloat)sp_navigationHeight {
return [self sp_safeAreaNavigationHeight] + 44;
}
- (CGFloat)sp_safeAreaTabbarHeight {
return [self sp_layoutInsets].bottom;
}
- (CGFloat)sp_tabbarHeight {
return [self sp_safeAreaTabbarHeight] + 49;
}
@end
2.通过C函数获取
.h
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
NS_ASSUME_NONNULL_BEGIN
@interface UIScreenHeight : NSObject
extern CGFloat Sta_Height(void);
extern CGFloat Nav_Height(void);
extern CGFloat Tab_Safe_Height(void);
extern CGFloat Tab_Height(void);
extern CGFloat Screen_Width(void);
extern CGFloat Screen_Height(void);
extern CGFloat FixWidth(CGFloat width);
extern CGFloat FixHeight(CGFloat height);
@end
NS_ASSUME_NONNULL_END
.m
#import "UIScreenHeight.h"
@implementation UIScreenHeight
CGFloat Sta_Height(void) {
if (@available(iOS 13.0, *)) {
return [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;
} else {
return [[UIApplication sharedApplication] statusBarFrame].size.height;
}
}
CGFloat Nav_Height(void) {
return Sta_Height() + 44;
}
CGFloat Tab_Safe_Height(void) {
return Sta_Height() == 44.f ? 34.f : 0.f;
}
CGFloat Tab_Height(void) {
return Tab_Safe_Height() + 49;
}
CGFloat Screen_Width(void) {
return [UIScreen mainScreen].bounds.size.width;
}
CGFloat Screen_Height(void) {
return [UIScreen mainScreen].bounds.size.height;
}
CGFloat ScreenWidthRatio(void) {
return Screen_Width() / 375.0;
}
CGFloat ScreenHeightRatio(void) {
return Screen_Height() / 667.0;
}
CGFloat FixWidth(CGFloat width) {
return floorf((width) * ScreenWidthRatio() * 100)/100;
}
CGFloat FixHeight(CGFloat height) {
return floorf((height) * ScreenWidthRatio() * 100)/100;
}
@end
3.宏获取
// 适配
// 现在产品设计稿有以iPhone6为基准的
#define Screen_Width ([UIScreen mainScreen].bounds.size.width)
#define Screen_Height ([UIScreen mainScreen].bounds.size.height)
#define Screen_Bounds [UIScreen mainScreen].bounds
#define ScreenWidthRatio (Screen_Width / 375.0)
#define ScreenHeightRatio (Screen_Height / 667.0)
//#define HYWidth(x) (floorf((x) * HYScreenWidthRatio))
//#define HYHeight(x) (floorf((x) * HYScreenHeightRatio))
#define Width(x) (floorf((x) * ScreenWidthRatio * 100)/100)
#define Height(x) (floorf((x) * ScreenHeightRatio * 100)/100)
#define Sta_Height [[UIApplication sharedApplication] statusBarFrame].size.height
#define FullScreen Sta_Height==44.f
#define Nav_Height (FullScreen?88.f:64.f)
#define Tab_Safe_Height (FullScreen?34.f:0.f)
#define Tab_Height (Tab_Safe_Height+49.f)