1.button上的文字左對齊
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
2.tabbar、navigationBar的半透明相關(guān)(translucent
)
self.navigationController.navigationBar.translucent = NO;
解釋:
默認(rèn)是YES,如果設(shè)置成YES ,放一張不透明的圖,效果自動(dòng)會(huì)把這個(gè)圖弄成半透明;
如果設(shè)置成NO,放了一張半透明的圖,
如果barstyle是UIBarStyleBlack,效果是半透明的圖自動(dòng)加上黑色背景
如果barstyle是UIBarStyleDefault,效果是半透明的圖自動(dòng)加上白色背景
如果設(shè)置了barTintColor,效果是半透明的圖自動(dòng)加上barTintColor的背景
self.navigationController.navigationBar.barTintColor = RGB(255, 156, 187, 1);
大坑:設(shè)置之后self.navigationController.navigationBar.translucent = NO;
self.view的布局會(huì)向下移64
3.iOS7+后的默認(rèn)的布局將從navigation bar的頂部開始,默認(rèn)UIRectEdgeALL
//從navigation bar底部開始布局
self.edgesForExtendedLayout = UIRectEdgeNone
4.automaticallyAdjustsScrollViewInsets
屬性(自動(dòng)適應(yīng)ScrollView的inset,顧名思義)
//此屬性對ScrollView及其子類都有作用(tableview,collectionView)
self.automaticallyAdjustsScrollViewInsets = NO 時(shí)(默認(rèn)YES)
舉例:YES時(shí),若tableview是從屏幕的最上邊開始,也就是與導(dǎo)航欄狀態(tài)欄重疊時(shí),會(huì)自動(dòng)調(diào)整tableview從navigationBar底部開始顯示,這是Apple的人性化默認(rèn)設(shè)置,也讓很多新手在此崴過腳。。
如果不需要viewController來幫我們調(diào)整scrollView的inset那么在iOS7之后我們需要設(shè)置NO。
5.注意label每次調(diào)用 sizeToFit
會(huì)重新調(diào)整自己的size和文字的相等,并不是設(shè)置一次就完事
[_nameLabel sizeToFit];
//見名之意,調(diào)整字體尺寸來適應(yīng)固定寬度,默認(rèn)NO
_nameLabel.adjustsFontSizeToFitWidth = YES;
6.圖片轉(zhuǎn)化為data
UIImagePNGRepresentation(UIImage* image) ;
//JPEG的轉(zhuǎn)換方法里面第二個(gè)參數(shù)是壓縮系數(shù),可以有效的減小圖片的大小。
UIImageJPEGRepresentation(UIImage* image, 1.0)
UIImagePNGRepresentation
要比UIImageJPEGRepresentation
返回的圖片數(shù)據(jù)量大很多。項(xiàng)目中做圖片上傳之前,經(jīng)過測試同一張拍照所得照片png大小在8M,而JPG壓縮系數(shù)為0.75時(shí)候,大小只有1M。而且,將壓縮系數(shù)降低好像對圖片視覺上并沒有太大的影響。
7.帶參數(shù)的宏定義
#define TuiJianDetailUrl(userId) [@"http://xxxxx/userrecommends/user/" stringByAppendingFormat:@"%@",userId]
8.@try@catch有時(shí)打印不出錯(cuò)誤原因,但是可以定位到錯(cuò)誤的代碼:
NSArray *obj =[[NSArray alloc]init];
@try {
[obj objectAtIndex:2];//此中若有錯(cuò)誤,拋出異常到catch中
}
@catch (NSException *exception) //此中可以打印出異常
NSLog(@"name:%@,reason:%@",exception.name,exception.reason);
}
@finally {//無論成功與否,都會(huì)執(zhí)行的代碼
}
9.navigationItem隱藏相關(guān)
self.navigationItem = nil;
self.navigationItem.hidesBackButton = YES;
self.navigationItem.rightBarButtonItem.customView.hidden=YES;
10.判斷文字全部為空
- (BOOL) isBlankString:(NSString *)string {
if (string == nil || string == NULL||[string isEqualToString:@""]) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
判斷內(nèi)容是否全部為空格
+ (BOOL) isEmpty:(NSString *) str {
if (!str) {
return true;
} else {
//A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
//Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
if ([trimedString length] == 0) {
return true;
} else {
return false;
}
}
}
11.NavigationController堆棧內(nèi)的UIViewController可以支持右滑手勢返回,也就是不用點(diǎn)擊左上角的返回按鈕
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
這個(gè)功能是好,但是經(jīng)常我們會(huì)有需求定制返回按鈕,如果手動(dòng)定制了返回按鈕,這個(gè)功能將會(huì)失效,也就是自定義了navigationItem的leftBarButtonItem,那么這個(gè)手勢就會(huì)失效。解決方法
法1.重新設(shè)置手勢的delegate
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
法2.當(dāng)然你也可以自己響應(yīng)這個(gè)手勢的事件
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];
12.const
1.const NSString *coder = @"iOS";
"*coder"不能被修改, "coder"能被修改
2.NSString const *coder = @"iOS";
"*coder"不能被修改, "coder"能被修改
3.NSString * const coder = @"iOS";
"coder"不能被修改,"*coder"能被修改
注意:1和2其實(shí)沒什么區(qū)別
結(jié)論:const右邊的總不能被修改
- navigationBar背景圖添加,注意是UIBarMetricsDefault
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
14.removeFromSuperview
無論是ARC還是MRC中多次調(diào)用removeFromSuperview和addSubview:方法,都不會(huì)造成造成重復(fù)釋放和添加。
蘋果的官方API注明:Never call this method from inside your view’s drawRect: method.
譯:永遠(yuǎn)不要在你的View的drawRect:方法中調(diào)用removeFromSuperview。
void UIGraphicsBeginImageContext(CGSize size);
參數(shù)size為新創(chuàng)建的位圖上下文的大小。它同時(shí)是由UIGraphicsGetImageFromCurrentImageContext
函數(shù)返回的圖形大小。
該函數(shù)的功能同UIGraphicsBeginImageContextWithOptions
的功能相同,相當(dāng)與UIGraphicsBeginImageContextWithOptions
的opaque
參數(shù)為NO,scale
為1.0。
16.NSData.length 是Bytes單位
1K = 1024B(Bytes);
1M = 1024K;
17.給UIView設(shè)置圖片
UIImage*image = [UIImage imageNamed:@"playing"];
_layerView.layer.contents = (__bridgeid)image.CGImage;
//同樣可以設(shè)置顯示的圖片范圍
//不過此處略有不同,這里的四個(gè)值均為0-1之間;對應(yīng)的依然是寫x,y,widt,height
_layerView.layer.contentsCenter = CGRectMake(0.25,0.25,0.5,0.5);
18.cell添加簡單動(dòng)畫
-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
NSArray*array = tableView.indexPathsForVisibleRows;
NSIndexPath*firstIndexPath = array[0];
//設(shè)置anchorPoint
cell.layer.anchorPoint = CGPointMake(0,0.5);
//為了防止cell視圖移動(dòng),重新把cell放回原來的位置
cell.layer.position = CGPointMake(0,cell.layer.position.y);
//設(shè)置cell
// 按照z軸旋轉(zhuǎn)90度,注意是弧度
if(firstIndexPath.row < indexPath.row){
cell.layer.transform = CATransform3DMakeRotation(M_PI_2,0,0,1.0);
}else{
cell.layer.transform = CATransform3DMakeRotation(-M_PI_2,0,0,1.0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:0.4 animations:^{
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1.0;
}];
}
19.CAShapeLayer屬于Core Animation,通過GPU繪制,節(jié)省性能,效率高,優(yōu)化內(nèi)存;
drawRect屬于Core Graphics,通過CPU繪制,比較消耗性能。
20.區(qū)分模擬器和非模擬器
#if TARGET_IPHONE_SIMULATOR
#else
#endif
//如果是模擬器
//非模擬器
\t 水平制表符,相當(dāng)于Tab鍵 \n
換行
\b 空格
\f 換頁符
\r 回車
22.設(shè)置readonly的屬性還能改變其值么?能
readonly只讀的屬性可以用@synthesize聲明,再手動(dòng)創(chuàng)建其getter方法(無法賦值)
@property (readonly, strong, nonatomic) Store *store;
@synthesize store = _store;
- (Store *)store{
if (_store == nil) {
_store = [Store store];
}
return _store;
}
23.注釋失效Comand+/不能使用
升級Xcode8之后,使用Comand+/快捷鍵進(jìn)行注釋不管用了,如果你的Xcode是運(yùn)行在 OS X 10.11 El Capitan的話,stackoverflow上比較統(tǒng)一的解決方法就是sudo /usr/libexec/xpccachectl ,然后重啟電腦就可以了。
24.用xib做cell自動(dòng)估算行高:
//先使用xib約束好view與contentView的關(guān)系,保證內(nèi)容增加,會(huì)撐開contentView;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0; // 設(shè)置為一個(gè)接近于行高“平均值”的數(shù)值
25、dequeueReusableCellWithIdentifier兩種方法區(qū)別
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
原來的nullable是返回值可空,下面的方法是一定能返回一個(gè)cell的(前提是registerClass了),判空會(huì)報(bào)錯(cuò);
26.都執(zhí)行同一個(gè)方法
讓數(shù)組中的每個(gè)元素 都執(zhí)行同一個(gè)方法aMethod
makeObjectsPerformSelector:@select(aMethod)
//簡介:讓數(shù)組中的每個(gè)元素 都調(diào)用 aMethod 并把 withObject 后邊的 oneObject 對象做為參數(shù)傳給方法aMethod
makeObjectsPerformSelector:@select(aMethod)withObject:oneObject
27.分析內(nèi)存泄露(shift+command+b),Xcode的Analyze功能
28.歸檔解檔,又稱對象的序列化反序列化,是把對象以二進(jìn)制的形式存在硬盤或網(wǎng)絡(luò)上面。
29.UIButton去掉系統(tǒng)的按下高亮置灰效果 :
[plusButton setAdjustsImageWhenHighlighted:NO];
30.拼接字典:addEntriesFromDictionary:
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"BMW",@"CarLogo",@"Red",@"CarColor", nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Xiaoming",@"name",@"28", @"age",nil];
[dic1 addEntriesFromDictionary:dic2];