1圈盔、在ios中打開(kāi)鏈接地址的方法:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"url"]]];
2、禁止手勢(shì)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(@"當(dāng)前的手勢(shì):%@",gestureRecognizer);
NSLog(@"當(dāng)前的處理view:%@",touch.view);
if ([touch.view isKindOfClass:[UIButton class]]){
return NO;
}
return YES;
}
3悄雅、是否是子類
isMemberOfClass
4驱敲、代碼模擬用戶點(diǎn)擊
模擬UI的事件sendActionsForControlEvents
,比如模擬用戶點(diǎn)擊事件
[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];
當(dāng)執(zhí)行這句話的時(shí)候众眨,會(huì)造成
controller
的view
的坐標(biāo)不是以屏幕左上為原點(diǎn)握牧,而是以navigationbar下面為原點(diǎn)。
如圖:
6娩梨、IOS 字符串中去除特殊符號(hào) stringByTrimmingCharactersInSet
NSString *characterDecodedStr = @"[I LOVE YOU]";
NSString *abc = [characterDecodedStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"沿腰,。狈定?.,?\\r\\n\\t[]"]];
這個(gè)方法只能去除首字符和末字符的特殊字符颂龙,中間不能出去。
stringByTrimmingCharactersInSet
removes 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措嵌、時(shí)間戳timeIntervalSince1970
一、轉(zhuǎn)化時(shí)間戳方法:
NSString *timeSp = [NSString stringWithFormat:@"%d", (long) [localeDate timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //時(shí)間戳的值
二芦缰、把獲取的時(shí)間轉(zhuǎn)化為當(dāng)前時(shí)間
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來(lái)看下是什么格式
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:datenow];
NSDate *localeDate = [datenow dateByAddingTimeInterval: interval];
NSLog(@"%@", localeDate);
三企巢、時(shí)間戳轉(zhuǎn)換為時(shí)間的方法
NSDate *confirmTimesp = [NSDate dateWithTimeIntervalSince1970:136789745666];
NSLog(@"136789745666 = %@", confirmTimesp);
9、解決UIScrollView 產(chǎn)生64像素offset偏移問(wèn)題
遇到含導(dǎo)航欄的ViewController
饺藤,其第一個(gè)子試圖是UIScrollView
會(huì)自動(dòng)產(chǎn)生64
像素偏移包斑。 找了哈資料發(fā)現(xiàn)可以通過(guò)設(shè)置viewcontroller
的self.automaticallyAdjustsScrollViewInsets = false
解決了。
另外一個(gè)辦法就去Storyboard
上將ViewController
的Under Top Bar
勾去掉也可以解決
automaticallyAdjustsScrollViewInsets
:是否根據(jù)按所在界面的navigationbar
與tabbar
的高度涕俗,自動(dòng)調(diào)整scrollview
的 inset
罗丰。
10、NSDictionary 顯示出來(lái)的allkeys排序不正確.
解決方式再姑,先進(jìn)行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ū)分大小寫(xiě)比較
NSLiteralSearch
Exact character-by-character equivalence.區(qū)分大小寫(xiě)比較
NSBackwardsSearch
Search from end of source string.字符串末尾開(kāi)始搜索
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.忽略 "-" 符號(hào)的比較
NSWidthInsensitiveSearch
Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets.忽略字符串的長(zhǎng)度元镀,比較出結(jié)果
NSForcedOrderingSearch
Comparisons are forced to return either NSOrderedAscending or NSOrderedDescending
if the strings are equivalent but not strictly equal.忽略不區(qū)分大小寫(xiě)比較的選項(xiàng)绍填,并強(qiáng)制返回 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è)置此項(xiàng)讨永,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
11、UIScrolleView
的bounces
屬性
bounces
//默認(rèn)是 yes
遇革,就是滾動(dòng)超過(guò)邊界會(huì)反彈有反彈回來(lái)的效果卿闹。假如是 NO
,那么滾動(dòng)到達(dá)邊界會(huì)立刻停止
12萝快、如何巧妙隱藏一行 UITableViewCell
有些時(shí)候, 我們想動(dòng)態(tài)的隱藏某一行的UITableView里面某一行的Cell,一般我們會(huì)用到下面代碼去實(shí)現(xiàn)第三行Cell隱藏.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.row == 3 ? 0 : 40;}
但是很不幸的是, 我們有時(shí)候雖然把高度設(shè)置 0, 但是有時(shí)候Cell里面的Label的文字還是顯示, 還和其他Cell重疊.
解決方法
有很多方法去解決這個(gè)問(wèn)題, 只需要設(shè)置
UITableViewDelegate
里面添加下面代碼, 或者在你的繼承UITableViewCell
的子類里面把這個(gè)屬性設(shè)置YES
.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.clipsToBounds = YES;}
clipsToBounds
屬性
取值:BOOL(YES/NO)
作用:決定了子視圖的顯示范圍锻霎。具體的說(shuō),就是當(dāng)取值為YES
時(shí)揪漩,剪裁超出父視圖范圍的子視圖部分旋恼;當(dāng)取值為NO
時(shí),不剪裁子視圖奄容。默認(rèn)值為NO
冰更。
13产徊、NSArray
的containsObject
方法,比較是什么
- (BOOL)containsObject:(id)anObject
蘋(píng)果給的文檔里說(shuō)的是冬殃,當(dāng)判斷anObject是否在當(dāng)前的NSArray
中的時(shí)候囚痴,是通過(guò)調(diào)用isEqual:
這個(gè)方法來(lái)判斷的,即對(duì)于NSArray
中的每個(gè)對(duì)象都會(huì)調(diào)用一次isEqual:anObject
审葬,如果返回YES自然就是說(shuō)這個(gè)數(shù)組包含anObject
。那這個(gè)時(shí)候問(wèn)題可能就來(lái)了奕谭,比如說(shuō)這個(gè)問(wèn)題涣觉。究其原因的話還在于isEqual
:這個(gè)方法。
所以isEqual
:本質(zhì)是在比較兩個(gè)對(duì)象的hash血柳。蘋(píng)果也說(shuō)了:
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
的時(shí)候官册,可以子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)在蘋(píng)果提供了方法描述的快捷鍵option + command + /
17.iOS
取絕對(duì)值- Abs Fabs Fabsf
用法
int abs(int i)
; // 處理int類型的取絕對(duì)值
double fabs(double i)
; //處理double類型的取絕對(duì)值
float fabsf(float i)
; /處理float類型的取絕對(duì)值
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ù)更新。击敌。介返。。