1、在ios中打開鏈接地址的方法:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"url"]]];
2、禁止手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(@"當前的手勢:%@",gestureRecognizer);
NSLog(@"當前的處理view:%@",touch.view);
if ([touch.view isKindOfClass:[UIButton class]]){
return NO;
}
return YES;
}
3、是否是子類
isMemberOfClass
4、代碼模擬用戶點擊
模擬UI的事件sendActionsForControlEvents,比如模擬用戶點擊事件
[myBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
再比如模擬segmentedControl的titleChange事件
[self.segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];
5、全局設(shè)置navigationbar背景
UINavigationBar *navigationBar = [UINavigationBar appearance];
[navigationBar setBackgroundImage:[UIImage HJ_imageWithColor:UIColorFromRGB(CCMainThemeColorNumber())]
forBarMetrics:UIBarMetricsDefault];
當執(zhí)行這句話的時候,會造成
controller的view的坐標不是以屏幕左上為原點,而是以navigationbar下面為原點。
如圖:

6、IOS 字符串中去除特殊符號 stringByTrimmingCharactersInSet
NSString *characterDecodedStr = @"[I LOVE YOU]";
NSString *abc = [characterDecodedStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",。?.,?\\r\\n\\t[]"]];
這個方法只能去除首字符和末字符的特殊字符,中間不能出去。
stringByTrimmingCharactersInSetremoves characters from the beginning and end of your string, not from any place in it
7、判斷字符串是否包含某字符
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
return [self containsString:string];
} else {
return ([self rangeOfString:string].location != NSNotFound);
}
8、時間戳timeIntervalSince1970
一、轉(zhuǎn)化時間戳方法:
NSString *timeSp = [NSString stringWithFormat:@"%d", (long) [localeDate timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //時間戳的值
二、把獲取的時間轉(zhuǎn)化為當前時間
NSDate *datenow = [NSDate date];//現(xiàn)在時間,你可以輸出來看下是什么格式
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:datenow];
NSDate *localeDate = [datenow dateByAddingTimeInterval: interval];
NSLog(@"%@", localeDate);
三、時間戳轉(zhuǎn)換為時間的方法
NSDate *confirmTimesp = [NSDate dateWithTimeIntervalSince1970:136789745666];
NSLog(@"136789745666 = %@", confirmTimesp);
9、解決UIScrollView 產(chǎn)生64像素offset偏移問題
遇到含導(dǎo)航欄的ViewController,其第一個子試圖是UIScrollView會自動產(chǎn)生64像素偏移。 找了哈資料發(fā)現(xiàn)可以通過設(shè)置viewcontroller的self.automaticallyAdjustsScrollViewInsets = false
解決了。
另外一個辦法就去Storyboard上將ViewController 的Under Top Bar
勾去掉也可以解決
automaticallyAdjustsScrollViewInsets:是否根據(jù)按所在界面的navigationbar與tabbar的高度,自動調(diào)整scrollview的 inset。
10、NSDictionary 顯示出來的allkeys排序不正確.
解決方式,先進行allkeys排序,然后根據(jù)allkeys排序取值
NSArray *keys = [[dic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
for (NSString *key in keys) {
if ([[dic objectForKey:key] isKindOfClass:[NSArray class]]) {
[self.settingData addObject:[dic objectForKey:key]];
}
}
NSStringCompareOptions介紹
NSCaseInsensitiveSearch
A case-insensitive search.不區(qū)分大小寫比較
NSLiteralSearch
Exact character-by-character equivalence.區(qū)分大小寫比較
NSBackwardsSearch
Search from end of source string.字符串末尾開始搜索
NSAnchoredSearch
Search is limited to start (or end, if NSBackwardsSearch
) of source string.搜索限制范圍的字符串
NSNumericSearch
Numbers within strings are compared using numeric value, that is, Name2.txt < Name7.txt< Name25.txt
按照字符串里的數(shù)字為依據(jù),算出順序
NSDiacriticInsensitiveSearch
Search ignores diacritic marks.忽略 "-" 符號的比較
NSWidthInsensitiveSearch
Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets.忽略字符串的長度,比較出結(jié)果
NSForcedOrderingSearch
Comparisons are forced to return either NSOrderedAscending or NSOrderedDescending
if the strings are equivalent but not strictly equal.忽略不區(qū)分大小寫比較的選項,并強制返回 NSOrderedAscending 或者 NSOrderedDescending
NSRegularExpressionSearch
The search string is treated as an ICU-compatible regular expression. If set, no other options can apply except NSCaseInsensitiveSearch
and NSAnchoredSearch
. You can use this option only with the rangeOfString:...
methods and stringByReplacingOccurrencesOfString:withString:options:range:
.只能應(yīng)用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比較方法,如果設(shè)置此項,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
11、UIScrolleView的bounces屬性
bounces //默認是 yes,就是滾動超過邊界會反彈有反彈回來的效果。假如是 NO,那么滾動到達邊界會立刻停止
12、如何巧妙隱藏一行 UITableViewCell
有些時候, 我們想動態(tài)的隱藏某一行的UITableView里面某一行的Cell,一般我們會用到下面代碼去實現(xiàn)第三行Cell隱藏.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.row == 3 ? 0 : 40;}
但是很不幸的是, 我們有時候雖然把高度設(shè)置 0, 但是有時候Cell里面的Label的文字還是顯示, 還和其他Cell重疊.

解決方法
有很多方法去解決這個問題, 只需要設(shè)置
UITableViewDelegate里面添加下面代碼, 或者在你的繼承UITableViewCell的子類里面把這個屬性設(shè)置YES.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.clipsToBounds = YES;}
clipsToBounds屬性
取值:BOOL(YES/NO)
作用:決定了子視圖的顯示范圍。具體的說,就是當取值為YES時,剪裁超出父視圖范圍的子視圖部分;當取值為NO時,不剪裁子視圖。默認值為NO。
13、NSArray 的containsObject方法,比較是什么
- (BOOL)containsObject:(id)anObject
蘋果給的文檔里說的是,當判斷anObject是否在當前的NSArray中的時候,是通過調(diào)用isEqual:這個方法來判斷的,即對于NSArray中的每個對象都會調(diào)用一次isEqual:anObject,如果返回YES自然就是說這個數(shù)組包含anObject。那這個時候問題可能就來了,比如說這個問題。究其原因的話還在于isEqual:這個方法。

所以isEqual:本質(zhì)是在比較兩個對象的hash。蘋果也說了:
This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.
所以在使用constainobject的時候,可以子model中充血isequal
14.ios沙盒目錄
獲取目錄路徑的方法:
// 獲取沙盒主目錄路徑
NSString *homeDir = NSHomeDirectory();
// 獲取Documents目錄路徑
NSString *docDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// 獲取Library的目錄路徑
NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
// 獲取Caches目錄路徑
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// 獲取tmp目錄路徑
NSString *tmpDir = NSTemporaryDirectory();
獲取應(yīng)用程序程序包中資源文件路徑的方法:
NSLog(@"%@",[[NSBundle mainBundle] bundlePath]);
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
15.獲取自定義bundle
NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"My.bundle"];
// 或者這種
//NSString * path = [[NSBundle mainBundle] pathForResource:@"YPhotoBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *img_path = [bundle pathForResource:imgName ofType:@"png"];
16.方法注釋快捷鍵
現(xiàn)在蘋果提供了方法描述的快捷鍵option + command + /
17.iOS取絕對值- Abs Fabs Fabsf用法
int abs(int i); // 處理int類型的取絕對值
double fabs(double i); //處理double類型的取絕對值
float fabsf(float i); /處理float類型的取絕對值
18.iOS中的round、ceil、floor函數(shù)略解
round 如果參數(shù)是小數(shù),則求本身的四舍五入.
ceil 如果參數(shù)是小數(shù),則求最小的整數(shù)但不小于本身.
floor 如果參數(shù)是小數(shù),則求最大的整數(shù)但不大于本身.
Example:如何值是3.4的話,則
-- round 3.000000
-- ceil 4.000000
-- floor 3.00000
持續(xù)更新。。。。