当前位置:   article > 正文

iOS宏定义的使用与规范_ios17 navigationbar 高度 宏定义

ios17 navigationbar 高度 宏定义
摘要: 宏定义可以很方便开发和调试,我们也要对其进行归类,提高代码可读性和规范性。

宏定义在很多方面都会使用,例如定义高度、判断iOS系统、工具类,还有诸如文件路径、服务端api接口文档。为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中,下面我抛砖引玉,对一些常用的宏进行分类、分文件定义,希望对大家有所帮助。

  1. 定义尺寸类的宏

  1. DimensMacros.h
  2. //状态栏高度
  3. #define STATUS_BAR_HEIGHT 20
  4. //NavBar高度
  5. #define NAVIGATION_BAR_HEIGHT 44
  6. //状态栏 + 导航栏 高度
  7. #define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT))
  8. //屏幕 rect
  9. #define SCREEN_RECT ([UIScreen mainScreen].bounds)
  10. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  11. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
  12. #define CONTENT_HEIGHT (SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT)
  13. //屏幕分辨率
  14. #define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))
  15. //广告栏高度
  16. #define BANNER_HEIGHT 215
  17. #define STYLEPAGE_HEIGHT 21
  18. #define SMALLTV_HEIGHT 77
  19. #define SMALLTV_WIDTH 110
  20. #define FOLLOW_HEIGHT 220
  21. #define SUBCHANNEL_HEIGHT 62

2.定义沙盒目录文件的宏

  1. PathMacros.h
  2. //文件目录
  3. #define kPathTemp                   NSTemporaryDirectory()
  4. #define kPathDocument               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  5. #define kPathCache                  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  6. #define kPathSearch                 [kPathDocument stringByAppendingPathComponent:@"Search.plist"]
  7. #define kPathMagazine               [kPathDocument stringByAppendingPathComponent:@"Magazine"]
  8. #define kPathDownloadedMgzs         [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]
  9. #define kPathDownloadURLs           [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]
  10. #define kPathOperation              [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]
  11. #define kPathSplashScreen           [kPathCache stringByAppendingPathComponent:@"splashScreen"]
  12. #endif

3.工具类的宏

  1. UtilsMacros.h
  2. //Log utils marco
  3. #define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
  4. #ifdef DEBUG
  5. #define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
  6. #else
  7. #define DLog(...)
  8. #endif
  9. #ifdef DEBUG
  10. #define ULog(...)
  11. //#define ULog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
  12. #else
  13. #define ULog(...)
  14. #endif
  15. //System version utils
  16. #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
  17. #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
  18. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
  19. #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
  20. #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
  21. // 获取RGB颜色
  22. #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  23. #define RGB(r,g,b) RGBA(r,g,b,1.0f)
  24. #define IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
  25. #define IsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
  26. //角度转弧度
  27. #define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
  28. //大于等于7.0的ios版本
  29. #define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
  30. //大于等于8.0的ios版本
  31. #define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
  32. //iOS6时,导航VC中view的起始高度
  33. #define YH_HEIGHT (iOS7_OR_LATER ? 64:0)
  34. //获取系统时间戳
  35. #define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]

4.通知Notification相关的宏

  1. NotificationMacros.h
  2. //系统Notification定义
  3. #define TNCancelFavoriteProductNotification     @"TNCancelFavoriteProductNotification"      //取消收藏时
  4. #define TNMarkFavoriteProductNotification       @"TNMarkFavoriteProductNotification"        //标记收藏时
  5. #define kNotficationDownloadProgressChanged     @"kNotficationDownloadProgressChanged"      //下载进度变化
  6. #define kNotificationPauseDownload              @"kNotificationPauseDownload"               //暂停下载
  7. #define kNotificationStartDownload              @"kNotificationStartDownload"               //开始下载
  8. #define kNotificationDownloadSuccess            @"kNotificationDownloadSuccess"             //下载成功
  9. #define kNotificationDownloadFailed             @"kNotificationDownloadFailed"              //下载失败
  10. #define kNotificationDownloadNewMagazine        @"kNotificationDownloadNewMagazine"

服务端API接口的宏

  1. APIStringMacros.h
  2. //
  3. //接口名称相关
  4. #ifdef DEBUG
  5. //Debug状态下的测试API
  6. #define API_BASE_URL_STRING     @"http://boys.test.companydomain.com/api/"
  7. #else
  8. //Release状态下的线上API
  9. #define API_BASE_URL_STRING     @"http://www.companydomain.com/api/"
  10. #endif
  11. //接口
  12. #define GET_CONTENT_DETAIL      @"channel/getContentDetail" //获取内容详情(含上一个和下一个)
  13. #define GET_COMMENT_LIST        @"comment/getCommentList"   //获取评论列表
  14. #define COMMENT_LOGIN           @"comment/login"            //获取评论列表
  15. #define COMMENT_PUBLISH         @"comment/publish"          //发布评论
  16. #define COMMENT_DELETE          @"comment/delComment"       //删除评论
  17. #define LOGINOUT                @"common/logout"            //登出

还有很多其他类型的宏,此处不一一列举

创建一个import所有宏相关的文件Macros.h

  1. Macros.h
  2. #import "UtilsMacros.h"
  3. #import "APIStringMacros.h"
  4. #import "DimensMacros.h"
  5. #import "NotificationMacros.h"
  6. #import "SharePlatformMacros.h"
  7. #import "StringMacros.h"
  8. #import "UserBehaviorMacros.h"
  9. #import "PathMacros.h"

在xcode项目的pch文件中,导入Macros.h文件

  1. XcodeProjectName-Prefix.pch
  2. #ifdef __OBJC__
  3.     #import <UIKit/UIKit.h>
  4.     #import <Foundation/Foundation.h>
  5.     #import "Macros.h"
  6. #endif
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/223764
推荐阅读
相关标签
  

闽ICP备14008679号