----采自博客or文章
<1> 如果我們想在控制臺(tái)中輸出視圖的層級(jí)結(jié)構(gòu)描述信息,可以使用UIView的私有方法recursiveDescription,如圖1所示输吏。這個(gè)方法在文檔中找不到,在代碼中調(diào)用也會(huì)報(bào)錯(cuò)替蛉。
<2> iOS10新變化---廢棄的openURL
iOS 10之前用法:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];```
options 字典參數(shù)贯溅,沒(méi)有限定值就要傳空字典:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];```
鍵:UIApplicationOpenURLOptionUniversalLinksOnly
字符串型,Value:true或 false
( 注:value 傳Yes 或 No 有點(diǎn)糊涂躲查,待大神解答再做完善...... )
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:nil];
<3> 回顧下NSObject類的+load和+initialize這兩個(gè)方法吧它浅。
蘋果文檔
在程序啟動(dòng)時(shí),Runtime會(huì)去加載所有的類镣煮。在這一時(shí)期姐霍,如果類或者類的分類實(shí)現(xiàn)了+load方法,則會(huì)去調(diào)用這個(gè)方法。
而+initialize方法是在類或子類第一次接收消息之前會(huì)被調(diào)用镊折,這包括類的實(shí)例對(duì)象或者類對(duì)象胯府。如果類一直沒(méi)有被用到,則這個(gè)方法不會(huì)被調(diào)用恨胚。
基于這兩個(gè)方法的特殊性骂因,我們可以將類使用時(shí)所需要的一些前置條件在這兩個(gè)方法中處理。不過(guò)赃泡,如果可能寒波,應(yīng)該盡量放在+initialize中。因?yàn)?load方法是在程序啟動(dòng)時(shí)調(diào)用升熊,勢(shì)必會(huì)影響到程序的啟動(dòng)時(shí)間俄烁。而+initialize方法可以說(shuō)是懶加載調(diào)用,只有用到才會(huì)去執(zhí)行级野。
<4> Apple在去年推出了Objective-C的幾個(gè)新語(yǔ)言特性页屠,其中一個(gè)是__kindof關(guān)鍵字。這個(gè)關(guān)鍵字的意思是指定的對(duì)象是類A或者其子類的實(shí)例蓖柔。
我們可以看圖1代碼卷中,在這種情況下,雖然array中有第一個(gè)元素是UIView 的實(shí)例渊抽,但編譯器會(huì)給出警告蟆豫,因?yàn)榫幾g器認(rèn)為array中存儲(chǔ)的都是UIView 對(duì)象。
而如果我們用__kindof來(lái)修飾泛型類型懒闷,則編譯器不會(huì)有警告十减,其認(rèn)為array中存儲(chǔ)的是UIView 或其子類的對(duì)象。
__kindof的提出應(yīng)該與Objective-C泛型有很大關(guān)系愤估,在系統(tǒng)庫(kù)中存在很多這樣的場(chǎng)景帮辟,如UIView的subviews屬性,
參考鏈接:how to use __kindof in Objective-C
<5> 應(yīng)該使用FOUNDATION_EXPORT還是#define來(lái)定義常量?
FOUNDATION_EXPORT NSString * const kMyConstantString; FOUNDATION_EXPORT NSString * const kMyConstantString2;
還有一種常用的#define方法
#define kMyConstantString @"Hello"
區(qū)別:
使用第一種方法在檢測(cè)字符串的值是否相等的時(shí)候更快玩焰。對(duì)于第一種可以直接使用(stringInstance == MyFirstConstant)來(lái)比較,而define則使用的是這種([stringInstance isEqualToString:MyFirstConstant])由驹。第一種直接比較的指針地址,而第二個(gè)則是逐項(xiàng)比較字符昔园。
<6> 解決自定義返回按鈕后側(cè)邊返回失效
self.navigationController.interactivePopGestureRecognizer.delegate = self;
這個(gè)api功能就是在NavigationController堆棧內(nèi)的UIViewController可以支持右滑手勢(shì)蔓榄,
<7> iOS 網(wǎng)絡(luò)請(qǐng)求URL包含中文轉(zhuǎn)碼的解決辦法
ios9之前
NSString *newUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//該方法iOS 9 已經(jīng)廢棄了 但是如果你的項(xiàng)目需要兼容到iOS8或之前建議還是使用此方法
ios9之后
NSString *newUrlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
<8> iOS 跳轉(zhuǎn)到App store 評(píng)分 頁(yè)面
換成自己的id號(hào)
//https://itunes.apple.com/us/app/wo-zheng-ting-ju-ting-mei/id1146314461?l=zh&ls=1&mt=8
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",@"1146314461"];
NSURL * url = [NSURL URLWithString:str];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
else
{
NSLog(@"can not open");
}
<9> iOS開發(fā),UIButton怎樣去掉高亮透明效果
要求:長(zhǎng)按btn時(shí)(指的是按住btn不放),背景圖和選中時(shí)的狀態(tài)是一致的,但是試了各種方法都不行!
??????解決方法:
首先,創(chuàng)建一個(gè)button類,然后,重寫setHighlighted方法,里面不用寫任何東西;
- (void)setHighlighted:(BOOL)highlighted{
}
原文鏈接:http://www.reibang.com/p/d3510be7ca95
<10>讓滑動(dòng)返回手勢(shì)生效
如果使用自定義的按鈕去替換系統(tǒng)默認(rèn)返回按鈕,會(huì)出現(xiàn)滑動(dòng)返回手勢(shì)失效的情況默刚。解決方法也很簡(jiǎn)單甥郑,只需要重新添加導(dǎo)航欄的interactivePopGestureRecognizer的delegate即可。
首先為ViewContoller添加UIGestureRecognizerDelegate協(xié)議
然后設(shè)置代理
self.navigationController.interactivePopGestureRecognizer.delegate = self;
原文鏈接:http://www.reibang.com/p/31f177158c9e
<11>圖片寫入相冊(cè)薄
- (void)savePicToAlbum
{
UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:@""], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//此方法名隨便寫荤西,不過(guò)傳參必須一個(gè)不能少,否則報(bào)錯(cuò)
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
}else{
NSLog(@"save success澜搅!");
}
}
<12>SDWebImage遺留問(wèn)題(內(nèi)存暴增)
- (void)viewDidLoad {
//SDImageCache默認(rèn)是利用NSCache存儲(chǔ)資源伍俘,也就是利用內(nèi)存。設(shè)置不使用內(nèi)存就行
[[SDImageCache sharedImageCache] setShouldCacheImagesInMemory:NO];
[[SDImageCache sharedImageCache] setShouldDecompressImages:NO];
[[SDImageCache sharedImageCache] setShouldCacheImagesInMemory:NO];
// 設(shè)置內(nèi)存最大 1M
// The maximum length of time to keep an image in the cache, in seconds
[[SDImageCache sharedImageCache] setMaxMemoryCost:1024 * 1024 * 1];
[[SDImageCache sharedImageCache] setMaxCacheAge:3600 * 24 * 7];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[[SDImageCache sharedImageCache] clearMemory];
}
<13> 在某些時(shí)候AFN提供的數(shù)據(jù)解析類型可能不夠我們使用勉躺,比如某浪的數(shù)據(jù)結(jié)構(gòu)"text/plain"癌瘾,AFN友好的給我們提供了acceptableContentTypes接口,所以饵溅,我們可以通過(guò)更改這個(gè)屬性妨退,添加我們要解析的類型到集合中
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// Request type
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain",@"application/x-javascript", nil]];
<14>直接在xcode創(chuàng)建group會(huì)導(dǎo)致一個(gè)問(wèn)題,在真正的項(xiàng)目文件里實(shí)際上并沒(méi)有創(chuàng)建一個(gè)對(duì)應(yīng)的文件夾概说。正確的做法如圖:
(快捷鍵 command + option + a)
<15>for in遍歷數(shù)組注意點(diǎn):
最好不要試圖在for in中對(duì)數(shù)組進(jìn)行增刪碧注,采用常規(guī)的for循環(huán)
You send nextObject repeatedly to a newly created NSEnumerator object to have it return the next object in the original collection. When the collection is exhausted, nil is returned. You cannot “reset” an enumerator after it has exhausted its collection. To enumerate a collection again, you need a new enumerator.
The enumerator subclasses used by NSArray, NSDictionary, and NSSet retain the collection during enumeration. When the enumeration is exhausted, the collection is released.
NOTE
It is not safe to modify a mutable collection while enumerating through it. Some enumerators may currently allow enumeration of a collection that is modified, but this behavior is not guaranteed to be supported in the future.
大概的意思是說(shuō),快速遍歷的原理是根據(jù)enumerator對(duì)象內(nèi)部的計(jì)數(shù)器,調(diào)用nextObject方法來(lái)實(shí)現(xiàn)返回下一個(gè)數(shù)組元素的,直到元素全部返回就會(huì)返回nil,于是整個(gè)enumerator對(duì)象就遍歷完了;同時(shí)也提醒,以這種原理來(lái)遍歷enumerator對(duì)象的話,無(wú)論對(duì)這個(gè)對(duì)象做什么操作,對(duì)象的計(jì)數(shù)器都不會(huì)被重置!
注意下面的NOTE,建議最好不要再快速遍歷的時(shí)候修改enumerator,否則不保證是安全的.
原文鏈接:http://www.reibang.com/p/a634310774a9
<16> xcode 自帶顏色轉(zhuǎn)換插件:Colors
<17> 一句代碼移除所有的子視圖嚣伐,非 for loop方法
---- 巧用數(shù)組對(duì)元素的操作
[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
<18> 修改系統(tǒng)控件內(nèi)部屬性(runtime + KVC)
eg:UIPageControl指示器將圓點(diǎn)改成圖片
- 使用runtime遍歷UIPageControl結(jié)果如下打印
2017-03-13 10:45:35.772 uipage[3295:378446] UIPageControl: _lastUserInterfaceIdiom
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _indicators
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _currentPage
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _displayedPage
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _pageControlFlags
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _currentPageImage // 選中圖片
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _pageImage // 默認(rèn)圖片
2017-03-13 10:45:35.773 uipage[3295:378446] UIPageControl: _currentPageImages
2017-03-13 10:45:35.813 uipage[3295:378446] UIPageControl: _pageImages
2017-03-13 10:45:35.813 uipage[3295:378446] UIPageControl: _backgroundVisualEffectView
2017-03-13 10:45:35.813 uipage[3295:378446] UIPageControl: _currentPageIndicatorTintColor
2017-03-13 10:45:35.814 uipage[3295:378446] UIPageControl: _pageIndicatorTintColor
2017-03-13 10:45:35.814 uipage[3295:378446] UIPageControl: _legibilitySettings
2017-03-13 10:45:35.814 uipage[3295:378446] UIPageControl: _numberOfPages
19糖赔、橋接
__bridge type : 將CoreFoundation框架的對(duì)象所有權(quán)交給Foundation框架來(lái)使用,但是Foundation框架中的對(duì)象并不能管理該對(duì)象的內(nèi)存轩端。
__bridge_transfer Objective-C type : 將CoreFoundation框架的對(duì)象所有權(quán)交給Foundation來(lái)管理放典,如果Foundation中對(duì)象銷毀,那么我們之前的對(duì)象(CoreFoundation)會(huì)一起銷毀基茵。
NSString *firstName = (__bridge NSString *)(firstname);
NSString *lastName = (__bridge NSString *)(last name);
/***---------------------------------***/
NSString *firstName = (__bridge_transfer NSString *)(firstname);
NSString *lastName = (__bridge_transfer NSString *)(lastname);
20奋构、IOS(SystemConfiguration)框架中關(guān)于測(cè)試連接網(wǎng)絡(luò)狀態(tài)相關(guān)方法
- 注意:需要引入#include <netinet/in.h>
- (BOOL) connectedToNetwork
{
//創(chuàng)建零地址,0.0.0.0的地址表示查詢本機(jī)的網(wǎng)絡(luò)連接狀態(tài)
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
//獲得連接的標(biāo)志
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
//如果不能獲取連接標(biāo)志拱层,則不能連接網(wǎng)絡(luò)弥臼,直接返回
if (!didRetrieveFlags)
{
return NO;
}
//根據(jù)獲得的連接標(biāo)志進(jìn)行判斷
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable && !needsConnection) ? YES : NO;
}
21、檢測(cè)設(shè)備是否為iPhone X/XS/系列
檢測(cè)設(shè)備是否為 iPhone X/XS/XR 的幾種方式
22根灯、navi導(dǎo)航返回異常
因?yàn)橐恢笔褂?code>self.navigationController.navigationBarHidden或者
self.navigationController.navigationBar.hidden
來(lái)隱藏navigationBar
径缅。
換成:[self.navigationController setNavigationBarHidden:YES animated:animated];
帶動(dòng)畫的處理方式就可以解決問(wèn)題
23、iphone x tabbar 返回時(shí)布局錯(cuò)亂跳動(dòng)
這個(gè)問(wèn)題是 iOS 12.1 Beta 2 引入的問(wèn)題烙肺,只要 UITabBar 是磨砂的纳猪,并且 push viewController 時(shí) hidesBottomBarWhenPushed = YES 則手勢(shì)返回的時(shí)候就會(huì)觸發(fā),出現(xiàn)這個(gè)現(xiàn)象的直接原因是 tabBar 內(nèi)的按鈕 UITabBarButton 被設(shè)置了錯(cuò)誤的 frame桃笙,frame.size 變?yōu)?(0, 0) 導(dǎo)致的氏堤。
所以最簡(jiǎn)單的解決方案就是:
[UITabBar appearance].translucent = NO;
原文:https://blog.csdn.net/Longge_Li/article/details/83654333
24、UICollectionView 吸頂
layout.sectionHeadersPinToVisibleBounds = true