當前位置:
首頁 > 知識 > iOS開發 適配iPhoneX/iPhoneXr/iPhoneXs/iPhonexs max

iOS開發 適配iPhoneX/iPhoneXr/iPhoneXs/iPhonexs max

1、寫個單例

/**
.h文件
*/
@interface Singleton : NSObject
+(instancetype) shareInstance ;
@property (nonatomic, assign) CGFloat SafeAreaTopHeightX;//
@property (nonatomic, assign) CGFloat kStatusBarHeightX;//
@property (nonatomic, assign) CGFloat SafeAreaBottomHeightX;//
@end
/**
.m文件
*/
#import "Singleton.h"
static Singleton* _instance = nil;
@implementation Singleton
+(instancetype) shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init] ;
}) ;

return _instance ;
}
@end

2、在AppDelegate.m文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//必須寫在這個位置,在window初始化完成時
if (isIPhoneX()==1) {
[Singleton shareInstance].SafeAreaTopHeightX = 88.0f;
[Singleton shareInstance].SafeAreaBottomHeightX = 34.0f;
[Singleton shareInstance].kStatusBarHeightX = 44.0f ;

}else{
[Singleton shareInstance].SafeAreaTopHeightX = 64.0f;
[Singleton shareInstance].SafeAreaBottomHeightX = 0;
[Singleton shareInstance].kStatusBarHeightX = 20.0f ;
}

#pragma mark - 默認載入UI
LoginController *loginVC = [[LoginController alloc] init];
self.window.rootViewController = loginVC;
self.window.rootViewController = mainVC;

return YES;
}
static inline BOOL isIPhoneX() {
BOOL iPhoneX = NO;
/// 先判斷設備是否是iPhone/iPod
if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
return iPhoneX;
}

if (@available(iOS 11.0, *)) {
/// 利用safeAreaInsets.bottom > 0.0來判斷是否是iPhone X。
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
if (mainWindow.safeAreaInsets.bottom > 0.0) {
iPhoneX = YES;
}
}

return iPhoneX;
}

3、在.pch文件中 ,這樣定義宏

//導航欄高度
#define SafeAreaTopHeight [Singleton shareInstance].SafeAreaTopHeightX
//狀態欄
#define kStatusBarHeight [Singleton shareInstance].kStatusBarHeightX
//底部宏
#define SafeAreaBottomHeight [Singleton shareInstance].SafeAreaBottomHeightX

iOS開發 適配iPhoneX/iPhoneXr/iPhoneXs/iPhonexs max

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

C++ 中指針和引用的區別
C井處理html 標籤一些正則表達式 整理收集

TAG:程序員小新人學習 |