ios 獲取導航欄和狀態欄高度,針對iPhoneX


因為iPhoneX的產生,原本的導航欄再也不是44了,那么會出現什么問題呢?在隱藏導航欄的頁面,原本的高度要向上調整一個導航欄+狀態欄的高度。那么iPhoneX的導航欄高度不是44了,我們需要手動獲取,

有導航欄的,代碼如下:

1
2
3
4
5
6
//獲取狀態欄的rect
CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
//獲取導航欄的rect
CGRect navRect = self.navigationController.navigationBar.frame;
那么導航欄+狀態欄的高度
statusRect.size.height+navRect.size.height

是不是很簡單!!!!

對於項目有的很多處都需要這個代碼,可以給它寫成宏定義。如下

方法一:

1
2
//獲取導航欄+狀態欄的高度
#define getRectNavAndStatusHight  self.navigationController.navigationBar.frame.size.height+[[UIApplication sharedApplication] statusBarFrame].size.height

方法二:(用了宏定義的 參數返回)

1
2
3
4
5
6
//獲取導航欄+狀態欄的高度
#define getRectNavAndStatusHight \
({\
CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];\    CGRect rectNav = self.navigationController.navigationBar.frame;\
( rectStatus.size.height+ rectNav.size.height);\
})\

下面說說沒有導航欄(對於模態視圖推出):

#define isPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

首先判斷機型是不是iPhoneX,然后

如果是iPhoneX返回88 如果不是64


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM