iOS记事本

本文整理了iOS开发中的实用宏定义,包括View圆角设置、防止手机睡眠、禁用button高亮、处理TableView相关问题、GCD、通知、图片资源获取等,并提供了代码示例,帮助开发者提升效率。
摘要由CSDN通过智能技术生成

1、禁止手机睡眠
[UIApplication sharedApplication].idleTimerDisabled = YES;

2、隐藏某行cell

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{// 如果是你需要隐藏的那一行,返回高度为0 if(indexPath.row == YouWantToHideRow) return 0; return 44;}// 然后再你需要隐藏cell的时候调用[self.tableView beginUpdates];[self.tableView endUpdates];

3、禁用button高亮
button.adjustsImageWhenHighlighted = NO;或者在创建的时候 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

4、tableview遇到这种报错failed to obtain a cell from its dataSource
是因为你的cell被调用的早了。先循环使用了cell,后又创建cell。顺序错了可能原因:1、xib的cell没有注册 2、内存中已经有这个cell的缓存了(也就是说通过你的cellId找到的cell并不是你想要的类型),这时候需要改下cell的标识

5、cocoa pods报这个错误:unable to access 'https://github.com/facebook/pop.git/': Operation timed out after 0 milliseconds with 0 out of 0 bytes received
解决办法:原因可能是网络问题,网络请求超时了,只需要重试就行了

6、cocoa pods 出现ERROR: While executing gem ... (Errno::EPERM)
解决办法:https://segmentfault.com/q/1010000002926243

7、动画切换window的根控制器
// options是动画选项[UIView transitionWithView:[UIApplication sharedApplication].keyWindow duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ BOOL oldState = [UIView areAnimationsEnabled]; [UIView setAnimationsEnabled:NO]; [UIApplication sharedApplication].keyWindow.rootViewController = [RootViewController new]; [UIView setAnimationsEnabled:oldState]; } completion:^(BOOL finished) { }];

8、去除数组中重复的对象
NSArray *newArr = [oldArr valueForKeyPath:@“@distinctUnionOfObjects.self"];

9、编译的时候遇到 no such file or directory: /users/apple/XXX
是因为编译的时候,在此路径下找不到这个文件,解决这个问题,首先是是要检查缺少的文件是不是在工程中,如果不在工程中,需要从本地拖进去,如果发现已经存在工程中了,或者拖进去还是报错,这时候需要去build phases中搜索这个文件,这时候很可能会搜出现两个相同的文件,这时候,有一个路径是正确的,删除另外一个即可。如果删除了还是不行,需要把两个都删掉,然后重新往工程里拖进这个文件即可

Sbuild phases

10、iOS8系统中,tableView最好实现下-tableView: heightForRowAtIndexPath:这个代理方法,要不然在iOS8中可能就会出现显示不全或者无法响应事件的问题
11、iOS8中实现侧滑功能的时候这个方法必须实现,要不然在iOS8中无法侧滑
// 必须写的方法,和editActionsForRowAtIndexPath配对使用,里面什么不写也行- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {}

12、三个通知
NSSystemTimeZoneDidChangeNotification监听修改时间界面的两个按钮状态变化UIApplicationSignificantTimeChangeNotification 监听用户改变时间 (只要点击自动设置按钮就会调用) NSSystemClockDidChangeNotification 监听用户修改时间(时间不同才会调用)

13、SDWebImage本地缓存有时候会害人。如果之前缓存过一张图片,即使下次服务器换了这张图片,但是图片url没换,用sdwebimage下载下来的还是以前那张,所以遇到这种问题,不要先去怼服务器,清空下缓存再试就好了。
14、上线前注意:
1)、删掉代码中所有的测试代码2)、如果后台有审核模式,提醒后台开启此模式3)、主流程再跑一跑4)、全局搜索waring,检查所有标记waring的地方

15、跳进app权限设置
// 跳进app设置 if (UIApplicationOpenSettingsURLString != NULL) { UIApplication *application = [UIApplication sharedApplication]; NSURL *URL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { [application openURL:URL options:@{} completionHandler:nil]; } else { [application openURL:URL]; } }

16、给一个view截图
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

17、开发中如果要动态修改tableView的tableHeaderView或者tableFooterView的高度,需要给tableView重新设置,而不是直接更改高度。正确的做法是重新设置一下tableView.tableFooterView = 更改过高度的view。为什么?其实在iOS8以上直接改高度是没有问题的,在iOS8中出现了contentSize不准确的问题,这是解决办法。
18、注意对象为nil的时候,调用此对象分类的方法不会执行
19、collectionView的内容小于其宽高的时候是不能滚动的,设置可以滚动:
collectionView.alwaysBounceHorizontal = YES;collectionView.alwaysBounceVertical = YES;

20、设置navigationBar上的title颜色和大小
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor youColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}]

21、颜色转图片

  • (UIImage *)cl_imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}

22、view设置圆角

define ViewBorderRadius(View, Radius, Width, Color)\[View.layer setCornerRadius:(Radius)];[View.layer setMasksToBounds:YES];[View.layer setBorderWidth:(Width)];[View.layer setBorderColor:[Color CGColor]] // view圆角

23、强/弱引用

define WeakSelf(type) __weak typeof(type) weak##type = type; // weak#define StrongSelf(type) __strong typeof(type) type = weak##type; // strong

24、由角度转换弧度

define DegreesToRadian(x) (M_PI * (x) / 180.0)

25、由弧度转换角度

define RadianToDegrees(radian) (radian*180.0)/(M_PI)

26、获取图片资源

define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

27、获取temp

define PathTemp NSTemporaryDirectory()

28、获取沙盒 Document

define PathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

29、获取沙盒 Cache

define PathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

30、GCD代码只执行一次

define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

31、自定义NSLog

ifdef DEBUG#define NSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTIONLINE, ##VA_ARGS)#else#define NSLog(...)#endif

32、Font

define FontL(s) [UIFont systemFontOfSize:s weight:UIFontWeightLight]#define FontR(s) [UIFont systemFontOfSize:s weight:UIFontWeightRegular]#define FontB(s) [UIFont systemFontOfSize:s weight:UIFontWeightBold]#define FontT(s) [UIFont systemFontOfSize:s weight:UIFontWeightThin]#define Font(s) FontL(s)

33、FORMAT

define FORMAT(f, ...) [NSString stringWithFormat:f, ## VA_ARGS]

34、在主线程上运行

define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

35、开启异步线程

define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

36、通知

define NOTIF_ADD(n, f) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(f) name:n object:nil]#define NOTIF_POST(n, o) [[NSNotificationCenter defaultCenter] postNotificationName:n object:o]#define NOTIF_REMV() [[NSNotificationCenter defaultCenter] removeObserver:self]

37、随机颜色

  • (UIColor *)RandomColor { NSInteger aRedValue = arc4random() % 255; NSInteger aGreenValue = arc4random() % 255; NSInteger aBlueValue = arc4random() % 255; UIColor *randColor = [UIColor colorWithRed:aRedValue / 255.0f green:aGreenValue / 255.0f blue:aBlueValue / 255.0f alpha:1.0f]; return randColor;}

38、获取window
+(UIWindow)getWindow { UIWindow win = nil; //[UIApplication sharedApplication].keyWindow; for (id item in [UIApplication sharedApplication].windows) { if ([item class] == [UIWindow class]) { if (!((UIWindow*)item).hidden) { win = item; break; } } } return win;}

39、修改textField的placeholder的字体颜色、大小
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

40、统一收起键盘
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

41、控制屏幕旋转,在控制器中写
/** 是否支持自动转屏 /- (BOOL)shouldAutorotate { return YES;}/* 支持哪些屏幕方向 /- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;}/* 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法) */- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;}

42、获取app缓存大小

  • (CGFloat)getCachSize { NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize]; //获取自定义缓存大小 //用枚举器遍历 一个文件夹的内容 //1.获取 文件夹枚举器 NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"]; NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青年没有路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值