从事iOS开发有些年月了,从最开始的磕磕绊绊,不知所措,到现在的遇到困难都能快速做出最佳方案处理,中间经历了不可或缺的痛苦。在项目开发中,本人有用印象笔记记录的习惯,所以很多重复出现的坑,很快迎刃而解,而不在同一个地方摔倒两次。为此,特意总结了一下开发中经常遇到的坑,有些可能和你形成共鸣,有些在你看来或许是小儿科,不喜勿喷。

1.XCode8的项目在xcode7运行报错:
The document “ViewController.xib” requires Xcode 8.0 or later. This version does not support documents saved in the Xcode 8 format. Open this document with Xcode 8.0 or later.

有两种方法解决这个问题:

1.你同事也升级Xcode8,比较推荐这种方式,应该迎接改变。

2.右击XIB或SB文件 -> Open as -> Source Code,删除xml文件中下面一行字段。

2.场景:tabbar左右pan手势切换,其中一个VC是UIPageViewController,这样会导致到pageView的时候不能切换tabbar,如何禁掉pageVC切换呢?

出于UIPageViewController和UItableView等产生手势冲突,我们往往要禁用其翻页手势,代码如下:

self.pageViewController.dataSource = nil;

网络上搜到的重写手势等方法,亲测无效,所以给出这个最简单粗暴的方法。

// tabbar的切换动画(一般不用哦)- (void)viewWillDisappear:(BOOL)animated{[super viewWillDisappear:animated];    CATransition *transition = [CATransition animation];[transition setDuration:1];[transition setType:@"fade"];[self.tabBarController.view.layer addAnimation:transition forKey:nil];
}
// 解决带有轮播图的手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{if (touch.view.frame.origin.y<100){return NO;}return YES;
}
//1,禁止.DS_store生成:
打开 “终端” ,复制黏贴下面的命令,回车执行,重启Mac即可生效。
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE//2,恢复.DS_store生成:
defaults delete com.apple.desktopservices DSDontWriteNetworkStores

3.FMDB根据条件查询数据库出现的错误:

解决办法:

4.后台数据中出现空格特殊字符:

问题:注意选项A...我竟然匹配不到这种字符, \r\n\t都不行
方案:中文全角空格...你想说什么...我转了下...\u3000  已解决!

5.浮点型取整问题:

//Objective-C拓展了C,自然很多用法是和C一致的。比如浮点数转化成整数,就有以下四种情况。
//1.简单粗暴,直接转化float f = 1.5; int a; a = (int)f; NSLog("a = %d",a);//输出结果是1。(int)是强制类型转化,丢弃浮点数的小数部分。
//2.高斯函数,向下取整float f = 1.6; int a; a = floor(f); NSLog("a = %d",a);
//输出结果是1。floor()方法是向下取整,类似于数学中的高斯函数 [].取得不大于浮点数的最大整数,对于正数来说是舍弃浮点数部分,对于复数来说,舍弃浮点数部分后再减1.//3.ceil函数,向上取整。float f = 1.5; int a; a = ceil(f); NSLog("a = %d",a);
//输出结果是2。ceil()方法是向上取整,取得不小于浮点数的最小整数,对于正数来说是舍弃浮点数部分并加1,对于复数来说就是舍弃浮点数部分.//4.通过强制类型转换四舍五入。float f = 1.5; int a; a = (int)(f+0.5); NSLog("a = %d",a);
6.关于block传值及数据同步总结:
A B C三个界面间C界面修改内容达到AB界面刷新最新的数据保持ABC数据同步:1.C到B可以用block回调传值 2.B界面到A界面只需在B界面Back的时候发出拉取数据并刷新cell即可解决数据不同步现象。
//在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak、循环引用、不能执行之类的警告。 有代码洁癖的孩子们很想消除他们, 今天就让我们来一次Fuck 警告!!//首先学会基本的语句#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//这里写出现警告的代码
#pragma clang diagnostic pop   //这样就消除了方法弃用的警告!
7.iOS8调用相机警告:
错误代码 :Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or
snapshot after screen updates.
IOS8多了一个样式UIModalPresentationOverCurrentContext,
IOS8中 presentViewController时请将控制器的modalPresentationStyle设置为 UIModalPresentationOverCurrentContext, 问题解决!!

8.错误点: ENABLE_BITCODE错误设置(mrc下)
解决方法:
// 默认选中第一行
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
// 实现了选中第一行的方法
[self tableView:_mainIndustryTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
例如:
// 默认下选中状态
- (void)customAtIndex:(UITableView *)tableView
{// 默认选中第一行[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];if ([tableView isEqual:_mainIndustryTableView]) {[self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];}
}

9.iOS headerview与tableview之间距离控制?

//view 作为 tableView 的 tableHeaderView,单纯的改变 view 的 frame 是无济于事的,tableView  不会大度到时刻适应它的高度(以后 Apple 会不会改变就不知道了),
//所以,如何告诉tableView 它的 tableHeaderView 已经改变了?很简单,就一句话(关键最后一句):
[webView sizeToFit];
CGRect newFrame = headerView.frame;
newFrame.size.height = newFrame.size.height + webView.frame.size.height;
headerView.frame = newFrame;
[self.tableView setTableHeaderView:headerView];//这样以后,效果就出来了。不过这种过度显得有些生硬,能不能加一点点动画,让它变得顺眼一些呢?试试下面的代码:
[self.tableView beginUpdates];
[self.tableView setTableHeaderView:headerView];
[self.tableView endUpdates];

10.cell 分割线不全:

-(void)viewDidLayoutSubviews {if ([_listTableView respondsToSelector:@selector(setSeparatorInset:)]) {[_listTableView setSeparatorInset:UIEdgeInsetsZero];}if ([_listTableView respondsToSelector:@selector(setLayoutMargins:)])  {[_listTableView setLayoutMargins:UIEdgeInsetsZero];}}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {[cell setLayoutMargins:UIEdgeInsetsZero];}if ([cell respondsToSelector:@selector(setSeparatorInset:)]){[cell setSeparatorInset:UIEdgeInsetsZero];}
}// 自绘分割线
- (void)drawRect:(CGRect)rect
{CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);CGContextFillRect(context, rect);CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0xE2/255.0f green:0xE2/255.0f blue:0xE2/255.0f alpha:1].CGColor);CGContextStrokeRect(context, CGRectMake(0, rect.size.height - 1, rect.size.width, 1));
}
11.iOS7.0以后的UILabel会自动将Text行尾的空白字符全部去除,除了常见的半角空格(\0×20)和制表符(\t)之外,全角空格 (\u3000)也被计算在内,甚至连多余的换行符(\r,\n)也被自动去除了。
这一点虽然方便直接将控件赋值和无需取值后再trim,但是太过智能化 了之后,往往不能满足一些本可以简单实现的需求。
需求1.使用添加\n方式将上下文本连续空两行,即实现文本的2倍行距。
iOS7.0 之前解决办法:在每个换行符后面添加一个空格
即如果要显示为:
aaaaaaa
空行
空行
bbbbbb
使用以下格式进行文本赋值
lbl.text = @"aaaaaaa\n\u0020\n\u0020bbbbbb";
iOS7.0 之后需要增加 , 不增加则无效
lbl.numberOfLines = 0; // 0 表示行数不固定
lbl.lineBreakMode=UILineBreakModeWordWrap; // 允许换行(可选)
需求2.在所有的UILabel的text后增加一个空格,并使text右对齐。
iOS7.0 之前解决办法:直接在 text 后增加空格即可,即 text 在赋值前增加空格。
lbl.text = [NSString stringWithFormat:@"%@%@","aaaaa","\u0020"];
iOS7.0之后需要重写UILabel的drawTextInRect方法,通过缩短默认文本绘制Rect 的宽度半个字体宽度来实现。(当然也可以在底部铺一个view调整,暨简单又高效)
具体实现代码如下 :

#import "MyLabel.h"@implementation MyLabel
-(id) initWithFrame:(CGRect)frame { self = [super initWithFrame:frame];if(self){return self;}
}-(void) drawTextInRect:(CGRect)rect {//从将文本的绘制Rect宽度缩短半个字体宽度//self.font.pointSize / 2return [super drawTextInRect:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width - self.font.pointSize / 2, rect.size.height)];
}
@end
//附录:
//UILabel会自动清除的空白字符(UNICODE)
\u0009 CHARACTER TABULATION
\u000A LINE FEED
\u000D CARRIAGE RETURN
\u0020 SPACE
\u0085 NEXT LINE
\u00A0 NBSP
\u1680 OGHAM SPACE MARK
\u180E MONGOLIAN VOWEL SEPARATOR
\u2000 EN QUAD
\u200A HAIR SPACE
\u200B ZERO WIDTH SPACE
\u2028 LINE SEPARATOR
\u2029 PARAGRAPH SEPARATOR
\u202F NARROW NO-BREAK SPACE
\u205F MEDIUM MATHEMATICAL SPACE
\u3000 IDEOGRAPHIC SPACE 

12.监听UITextField的text的变化:

// 注册监听[[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidChangeNotification object:nil];[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeForKeyWord:) name:UITextFieldTextDidChangeNotification object:nil];
// 监听关键词变化
- (void)changeForKeyWord:(NSNotification *)sender
{// 关键词改变时清除地区查询条件纪录[[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"proRow"];[[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"section"];
}
//监听UITextField的点击事件
[[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterEdited:) name:UITextFieldTextDidBeginEditingNotification object:nil]; - (void)enterEdited:(NSNotification *)sender
{ //事件写这里!希望帮到你!
} 

13.改变cell的选中颜色:

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = COLOR_BACKGROUNDVIEW;
//不需要任何颜色可以这么设置:
cell.selectionStyle = UITableViewCellSelectionStyleNone;

14.旋转图片:

#pragma mark ----- 更新按钮动画
- (void)rotate360DegreeWithImageViews:(UIImageView *)myViews{CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];rotationAnimation.duration = 1.0;rotationAnimation.cumulative = YES;rotate360DegreeWithImageViewsrotationAnimation.repeatCount = 100000;[myViews.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
[myViews.layer removeAllAnimations]; // 停止
15.UIView的exclusiveTouch属性:
通过设置[self setExclusiveTouch:YES];
可以达到同一界面上多个控件接受事件时的排他性,从而避免一些问题。
//1. 设置的时候在ib里面记得选择无边框的,要不然随便你设置,都是无效的,也是坑死了。_textBoxName.layer.borderWidth=1.0f;_textBoxName.layer.borderColor=[UIColorcolorWithRed:0xbf/255.0fgreen:0xbf/255.0fblue:0xbf/255.0falpha:1].CGColor;//2.在uitextfield 中文字最左边距离左侧边框的距离_textBoxName.leftView=[[UIViewalloc] initWithFrame:CGRectMake(0,0, 16,51)];_textBoxName.leftViewMode=UITextFieldViewModeAlways;

16.当你使用 UISearchController 在 UITableView 中实现搜索条,在搜索框已经激活并推入新的 VC 的时候会发生搜索框重叠的情况:

解决办法:那就是 definesPresentationContext 这个布尔值。

17.画个曲线如何做呢?如图:

 UIView *myCustomView = [[UIView alloc]initWithFrame:CGRectMake(0, 204,kScreenWidth, 120)];myCustomView.backgroundColor = [UIColor whiteColor];[view addSubview:myCustomView];UIBezierPath *bezierPath = [UIBezierPath bezierPath];[bezierPath moveToPoint:CGPointMake(0,0)];[bezierPath addCurveToPoint:CGPointMake(myCustomView.width, 0) controlPoint1:CGPointMake(0, 0) controlPoint2:CGPointMake(myCustomView.width/2, 40)];[bezierPath addLineToPoint:CGPointMake(myCustomView.width, myCustomView.height)];[bezierPath addLineToPoint:CGPointMake(0, myCustomView.height)];[bezierPath closePath];CAShapeLayer *shapLayer = [CAShapeLayer layer];shapLayer.path = bezierPath.CGPath;myCustomView.layer.mask = shapLayer;myCustomView.layer.masksToBounds = YES;

18.有效解决刷新单个cell或者section闪一下的问题:

[UIView setAnimationsEnabled:NO];
[_listTable beginUpdates];
[_listTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
[_listTable endUpdates];
[UIView setAnimationsEnabled:YES];

19.保持imageView 图片不变形:

_topImageView.contentMode = UIViewContentModeScaleAspectFit;
[__NSArrayI addObject:]: unrecognized selector sent to instance
//当我创建了一个NSMutableArray 对象的时候
@property (nonatomic,copy)NSMutableArray *children;
//然后通过addObject运行就会报错,[__NSArrayI addObject:]: unrecognized selector sent to instance
//解决方法:copy改成strong

20.Label后加小图标:

NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",fields.title]];// 添加表情NSTextAttachment *attch = [[NSTextAttachment alloc] init];// 表情图片attch.image = [UIImage imageNamed:@"newTopList"];// 设置图片大小attch.bounds = CGRectMake(10, 0, 25, 14);if ([fields.isnew boolValue]) {// 创建带有图片的富文本NSAttributedString *strings = [NSAttributedString attributedStringWithAttachment:attch];[attri appendAttributedString:strings];}// 用label的attributedText属性来使用富文本_titleLabel.attributedText = attri;

21.状态栏字体颜色及背景颜色调整:

UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
statusBarView.backgroundColor= [UIColor whiteColor];
[self.view addSubview:statusBarView];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];

22.xib加载不同尺寸的屏幕如何控制宽高?

- (void)viewDidLoad {[super viewDidLoad];myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 206)];[self.view addSubview:myView];
}
- (void)viewDidLayoutSubviews {[super viewDidLayoutSubviews];//在这里计算尺寸myView.myView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 206);
}// 或者修改如下:UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-48, 232)];//310GCVipGroupView *groupView = [[GCVipGroupView alloc]initWithFrame:view.frame andCollect:NO];groupView.delegate = self;groupView.bigView.frame = view.frame;[view addSubview:groupView];

23.我的位置(强制获取):

MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];
// 当前经纬度
float currentLatitude = mylocation.placemark.location.coordinate.latitude;
float currentLongitude = mylocation.placemark.location.coordinate.longitude;// 默认位置(模拟器测试要注释掉才行)
[self setMapViewCenter:CLLocationCoordinate2DMake(currentLatitude, currentLongitude)];

24.比如弹框上放了scrollowView第一次弹出需要裁剪,滑动时需要显示下面的内容:

解决办法:让scrollowView的范围跟父视图同等高就解决了!

25.去除多余cell不管用怎么办:

self.searchResultTableView.tableFooterView = [[UIView alloc]init];
//或者加一个:    self.searchResultTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

26.判断页面消失或出现时是push还是pop操作:

- (void)viewWillDisappear:(BOOL)animated {NSArray *viewControllers = self.navigationController.viewControllers;//获取当前的视图控制其if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {//当前视图控制器在栈中,故为push操作NSLog(@"push");} else if ([viewControllers indexOfObject:self] == NSNotFound) {//当前视图控制器不在栈中,故为pop操作NSLog(@"pop");}
}

27.运行环境问题:

A valid provisioning profile for this executable was not found.
解决问题所在:发布证书无法运行在真机上!!!

每周更新关注:http://weibo.com/hanjunqiang  新浪微博!手机加iOS开发者交流QQ群: 446310206

iOS开发中 经常遇到的坑,看我就够了! 韩俊强的博客相关推荐

  1. iOS中 语音识别功能/语音转文字教程具体解释 韩俊强的博客

    前言:近期研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人针对两者的看法,讯飞毫无疑问比較专业.识别率也非常高真对语音识别是比較精准的,可是非常多开发人员和我一样期望离线识别,而讯飞离 ...

  2. iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客

    原文地址:http://blog.csdn.net/qq_31810357/article/details/51111702 前言:最近研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人 ...

  3. iOS开发中的零碎知识点笔记 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 1.关联 objc_setAssociatedObject关联是指把两个对象相互关联起来,使得其中的一个对象作为另外 ...

  4. iOS中 支付宝钱包详解/第三方支付 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址 ...

  5. iOS中 Realm的学习与使用 韩俊强的博客

    iOS开发者交流QQ群:446310206  有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 所以重新做了一份 后面看到官网的中文文 ...

  6. iOS中 为 iOS 建立 Travis CI 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang新浪微博! 你是否曾经试着为 iOS 项目搭建一台支持持续集成的服务器,从我的个人经验而言,这可不是一个轻松的活.首先需要准备一台 ...

  7. iOS中 UITableViewCell cell划线那些事 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang 在开发中经常遇到cell分割线显示不全或者想自定义线的宽高等; 最近总结了一下,希望帮到大家: 1.不想划线怎么办? Table ...

  8. iOS中 HeathKit框架学习 步数统计等 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博!iOS开发者交流QQ群: 446310206 HeathKit框架学习 本文结构 简介 用户数据安全及隐私 Heat ...

  9. iOS中 本地通知/本地通知详解 韩俊强的博客

    布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 Notification是智能手机应用编 ...

最新文章

  1. Android创建数据表和LitePal的基本用法
  2. java中methods方法_java中Class.getMethod方法
  3. 自定义php报错信息,自定义PHP的错误报告处理方式
  4. 奇异值分解(SVD)原理详解及推导(转载)
  5. RPC(远程过程调用)原理及应用
  6. 在 Delphi 下使用 DirectSound (2): 查看设备性能
  7. 2010年ImagineCup,我们共同走过
  8. react native与夜神模拟器结合使用运行安卓平台
  9. Camtasia Studio 8
  10. ASAN和HWASAN原理解析
  11. 粤嵌实习-linux下madplay播放器的下载和使用、线程的介绍和创建一个广告循环播放线程
  12. 云南省计算机一级考试题7,计算机(一级B类)云南省计算机一级考试题库.doc
  13. PHP EOF使用说明
  14. Python pygame 坦克大战
  15. vscode连接模拟器运行flutter项目
  16. 严重: Allocate exception for servlet CustomerServlet java.lang.ClassNotFoundException: cn.itcast.cstm.
  17. python代码 学生上课签到表_一种上课签到系统的制作方法
  18. JLINK的SWD接口调试器制作
  19. 在 Excel 启动时运行宏
  20. Picture2Epub

热门文章

  1. 北京博奥智源科技有限公司分析关于系统开发渗透测试
  2. 笔记:linux fastDFS搭建及设置自启动
  3. 安卓开发问题一:在虚拟机上运行APP不能运行,即闪退现象
  4. [Vue3实操] 开发Todo List
  5. 酒厂酒业IP网络广播系统建设方案-基于局域网的新一代交互智慧酒厂酒业IP广播设计指南
  6. php 随机播放音乐,听音乐时随机播放真的是随机的吗?
  7. uipath 使用datascraping获取表格数据
  8. Aurigma图片上传程序是为您的网站的图片上传解决方案
  9. GPT系列模型技术路径演进
  10. 响应时间(RT)、吞吐量(TPS)、QPS、并发数概念