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 = [NSStringstringWithFormat:@"%@%@","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 / 2

return [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;rotate360DegreeWithImageViews

rotationAnimation.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)];//310

GCVipGroupView *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.

解決問題所在:發布證書無法運行在真機上!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容