社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
从事iOS开发有些年月了,从最开始的磕磕绊绊,不知所措,到现在的遇到困难都能快速做出最佳方案处理,中间经历了不可或缺的痛苦。在项目开发中,本人有用印象笔记记录的习惯,所以很多重复出现的坑,很快迎刃而解,而不在同一个地方摔倒两次。为此,特意总结了一下开发中经常遇到的坑,有些可能和你形成共鸣,有些在你看来或许是小儿科,不喜勿喷。
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
解决办法:
4.后台数据中出现空格特殊字符:
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);
//在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak、循环引用、不能执行之类的警告。 有代码洁癖的孩子们很想消除他们, 今天就让我们来一次Fuck 警告!!
//首先学会基本的语句
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//这里写出现警告的代码
#pragma clang diagnostic pop //这样就消除了方法弃用的警告!
// 默认选中第一行
[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));
}