iOS XIB使用Safe Area后在iOS9和10上面出現(xiàn)的問題和解決方案
1.多添加一個距離SuperView的約束
2.設置這個約束為>=距離
3.設置距離Safe Area的約束優(yōu)先級比距離SuperView約束優(yōu)先級低。(比如750)
開發(fā)注意添加一個控制器的方法
// 缺一不可否則有可能會導致意外崩潰
[self addChildViewController:otherVC];
[self.view addSubview:otherVC.view];
UIButton禁用系統(tǒng)的高亮狀態(tài)
[button setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"like"] forState: UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateSelected];
// 選中時候的高亮狀態(tài)
[button setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateSelected | UIControlStateHighlighted];
IQKeyboardManager修改toolbar上的Placeholder
#import "IQUIView+IQKeyboardToolbar.h"
textField3.placeholderText = @"This is the customised placeholder title for displaying as toolbar title";

xib中創(chuàng)建多個view屬性連接如果出現(xiàn)問題就查看某一個view的連接情況

Unbalanced calls to begin/end appearance transitions for <MainViewController: 0x7ab61a20>.
原因就是上次動畫還沒結束,然后又開始了新的動畫,導致下一個的頁面無法順利壓棧,這個只需要上一個頁面返回的時候不要做動畫或就可以了。
解決方法
1:去掉上一個頁面或當前界面動畫
2:當前界面動畫延遲
3:監(jiān)聽上一個界面或當前的動畫是否完成
參考:http://sdlqhjk.iteye.com/blog/1769057
TableView點擊狀態(tài)欄滑動到頂部無效時如何處理
點擊statusBar,tableView自動滑動到頂部的功能,其實這個功能系統(tǒng)已實現(xiàn),是需要tableView.scrollsToTop = true即可,但是我們經(jīng)常遇到一個viewController中多個scrollView和tableView的情況,這時要注意:
1、系統(tǒng)默認
scrollsToTop屬性是打開的,我們要將 除了想要具備自動滑動到頂部的視圖的 其他視圖的scrollsToTop屬性全部關掉scrollsToTop = false,否則,全部視圖的滑動到頂部的功能都將失效;
2、如果在一個父控制器中添加多個子控制器A,B,C ,想A控制器中的tableView實現(xiàn)自動滑動到頂部,則這時B,C控制器的tableView的scrollsToTop屬性要= false。
真機調(diào)試不打印log問題解決
ios9以前的 如果不加圖片的那句 在xcode8 會打印一些沒用的日志 但是你的系統(tǒng)要是ios10 如果真機調(diào)試 加了這句就不會打印 要是刪除了就能

顯示和隱藏mac文件命令
顯示隱藏文件
defaults write com.apple.finder AppleShowAllFiles -bool true
關閉顯示隱藏文件
defaults write com.apple.finder AppleShowAllFiles -bool false
modal跳轉(zhuǎn)
ApresentBpresentC
CdismissA 代碼
UIViewController *rootVC = self.presentingViewController;
while (rootVC.presentingViewController) {
rootVC = rootVC.presentingViewController;
}
[rootVC dismissViewControllerAnimated:YES completion:nil];
原理看這篇文章:http://www.itdecent.cn/p/e2572e83071e
圓角按鈕
代碼方法:
btn.layer.cornerRadius = 8.0;
xib

DerivedData路徑
/Users/xxx/Library/Developer/Xcode/DerivedData/
CornerStone靜態(tài)庫(.a文件)問題
首先打開軟件左上角 CornerStone-Preferences-SubVersion
第一個地方把對號去掉,第二個地方把.a那個刪除,然后save。
然后把你的.a文件放到本地的相應文件夾下,
但是 CornerStone(我的是v2.7.10 版本的) 里相應文件夾下看不到帶問號的.a文件的話,點擊上邊 View-Show Ignored Items
到這里就ok了,選中你的靜態(tài)庫 點擊底部Add 問號會變成A,然后就可以提交了。

http://blog.sina.com.cn/s/blog_5c91824f0102vdlr.html
修改光標顏色
textField.tintColor = [UIColor redColor];
image在xcode中設置不被渲染

appearance
UI_APPEARANCE_SELECTOR凡是屬性或方法后面有該宏標記的,都可以通過appearance方法統(tǒng)一設置。
利用KVC修改系統(tǒng)的TabBar
[self setValue:customTabBar forKeyPath:@"tabBar"];
可以在自定義的- (void)layoutSubviews中修改tabbar的frame。
分類中聲明@property
在分類中聲明@property, 只會生成方法的聲明, 不會生成方法的實現(xiàn)和帶有_下劃線的成員變量
控制器title設置
self.title = @"標題";
// 相當于下面兩句代碼
self.navigationItem.title = @"標題"; // 設置導航欄標題
self.tabBarItem.title = @"標題"; // 設置tabbar標題
tabbarController添加控制器注意
不要在tabbarController里面訪問子控制器的view,會導致提前創(chuàng)建view。
initialize
/**
* 當?shù)谝淮问褂眠@個類的時候會調(diào)用一次
*/
+ (void)initialize;
自己創(chuàng)建控制器xib
1.設置File's Owner
2.設置view
xib文字換行快捷鍵
option + return
Cell的selection為None時注意
當cell的selection為None的時候,點擊cell時內(nèi)部的控件不會進入高亮狀態(tài),如textLabel的highlightedTextColor就會失效。當cell的selection為Default的時候,點擊cell內(nèi)部的控件會進入高亮狀態(tài)。
控制器銷毀處理
當正在請求數(shù)據(jù)時還未請求回來,此時點擊返回銷毀控制器,如果請求回來的代碼里訪問了該控制器,會導致崩潰,解決方案是在dealloc里取消請求。
- (void)dealloc {
// 取消請求
[self.manager.operationQueue cancelAllOperations];
}
利用runtime查找UITextField的隱藏成員變量
#import <objc/runtime.h>
+ (void)initialize {
unsigned int count = 0;
// 拷貝出所有的成員變量列表
Ivar *ivars = class_copyIvarList([UITextField class], &count);
for (int i = 0; i < count; i++) {
// 取出成員變量
Ivar ivar = ivars[i];
// 打印成員變量名字
NSLog(@"%s", ivar_getName(ivar));
}
// 釋放
free(ivars);
}
textfield占位文字和光標顏色修改
修改占位文字顏色:
[self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
設置光標顏色和文字顏色一致:
self.tintColor = self.textColor;
button常用布局方法
[button layoutIfNeeded]; // 強制布局(強制更新子控件的frame)
[button.titleLabel sizeToFit]; // 讓按鈕內(nèi)部的label根據(jù)文字內(nèi)容來計算尺寸
關于viewWithTag的一點說明
用[父view viewWithTag:0]取到的view, 發(fā)現(xiàn)不是子view, 而是這個父view
遇到這種情況就換一種思路取子view,比如subViews,或者把tag值設置大一點,防止誤使用到蘋果的保留tag。
tableView默認值
如果不給tableView設置y值,默認是20,height如果不設置,默認少20。
背景view拉伸
可以設置背景圖片的slicing為Horizontal and Vertical

Cell代碼對屬性訪問的封裝
在cell的setModel:中對訪問屬性的處理代碼,放在model的get方法里。比如:
// .m文件
#import "XMGTopic.h"
@implementation XMGTopic
- (NSString *)create_time
{
// 日期格式化類
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 設置日期格式(y:年,M:月,d:日,H:時,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 帖子的創(chuàng)建時間
NSDate *create = [fmt dateFromString:_create_time];
if (create.isThisYear) { // 今年
if (create.isToday) { // 今天
NSDateComponents *cmps = [[NSDate date] deltaFrom:create];
if (cmps.hour >= 1) { // 時間差距 >= 1小時
return [NSString stringWithFormat:@"%zd小時前", cmps.hour];
} else if (cmps.minute >= 1) { // 1小時 > 時間差距 >= 1分鐘
return [NSString stringWithFormat:@"%zd分鐘前", cmps.minute];
} else { // 1分鐘 > 時間差距
return @"剛剛";
}
} else if (create.isYesterday) { // 昨天
fmt.dateFormat = @"昨天 HH:mm:ss";
return [fmt stringFromDate:create];
} else { // 其他
fmt.dateFormat = @"MM-dd HH:mm:ss";
return [fmt stringFromDate:create];
}
} else { // 非今年
return _create_time;
}
}
@end
MJExtension的使用
如果服務器返回的字段類型是int==0,1,接收屬性類型可以直接使用BOOL
setter和getter的實現(xiàn)
如果同時重寫了setter和getter方法,是不會生成私有變量,需要手動添加。
如果聲明為readonly并重寫了getter方法,也不會生成私有變量。
設置frame和顯示不一樣
如果出現(xiàn)這種情況就添加下面代碼
self.autoresizingMask = UIViewAutoresizingNone;
如果是在xib中將控件添加到scrollView中并沒有設置約束,用代碼去設置frame是不行的,這時需要用代碼添加到scrollView
如何知道圖片的真實類型
就是取出圖片數(shù)據(jù)的第一個字節(jié)
SDWebImage代碼:
+ (NSString *)sd_contentTypeForImageData:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"image/jpeg";
case 0x89:
return @"image/png";
case 0x47:
return @"image/gif";
case 0x49:
case 0x4D:
return @"image/tiff";
case 0x52:
// R as RIFF for WEBP
if ([data length] < 12) {
return nil;
}
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return @"image/webp";
}
return nil;
}
return nil;
}
屏蔽第三方框架帶來的風險
根據(jù)第三方框架包裝一個自己的類,面向自己的類開發(fā)
reason:-[NSInvocation setArgument:atIndex:]:Index(2) out of bounds[-1,1]
selector參數(shù)有錯誤,比如將圖片寫入相冊
// 錯誤
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(saveSuccess), nil);
// 正確
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// 官方解釋
// Adds a photo to the saved photos album. The optional completionSelector should have the form:
// - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo) __TVOS_PROHIBITED;
pop和CoreAnimation的區(qū)別
pop:https://github.com/facebook/pop
- CoreAnimation的動畫只能添加到layer上
- pop的動畫可以添加到任何對象
- pop的底層并非基于CoreAnimation,而是基于CADisplayLink
- CoreAnimation的動畫僅僅是表象,并不會修改對象的frame等值
- pop的動畫會實時修改對象的屬性,是真正的修改了對象的屬性
modal控制器注意
modal控制器后面的控制器會暫時移出窗口
window使用注意
window如果不想設置主窗口,只是顯示,只要hidden = NO就出來了
windowLevel級別:UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
約束動畫
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
// 鍵盤顯示\隱藏完畢的frame
CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 修改底部約束
self.bottomSapce.constant = XMGScreenH - frame.origin.y;
// 動畫時間
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 動畫 及時刷新
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
設置tableHeaderView的問題
設置tableHeaderView的時候,如果直接設置tableHeaderView為cell,且cell又重寫了setFrame方法,且重寫時候是這樣的重寫setFrame方法,那么在拖tableView的時候系統(tǒng)會修改headerView的frame會一直調(diào)這個方法,就會出現(xiàn)問題。
解決方案:就是設置headerView為cell的時候外面包一層view就可以避免這個問題。
重寫setFrame方法:
- (void)setFrame:(CGRect)frame
{
frame.origin.x = XMGTopicCellMargin;
frame.size.width -= 2 * XMGTopicCellMargin;
// frame.size.height -= XMGTopicCellMargin;
frame.size.height = self.topic.cellHeight - XMGTopicCellMargin;
frame.origin.y += XMGTopicCellMargin;
[super setFrame:frame];
}
設置tableHeaderView
- (void)setupHeader
{
// 創(chuàng)建header
UIView *header = [[UIView alloc] init];
// 清空top_cmt
if (self.topic.top_cmt.count) {
self.saved_top_cmt = self.topic.top_cmt;
self.topic.top_cmt = nil;
[self.topic setValue:@0 forKeyPath:@"cellHeight"];
}
// 添加cell
XMGTopicCell *cell = [XMGTopicCell cell];
cell.topic = self.topic;
cell.size = CGSizeMake(XMGScreenW, self.topic.cellHeight);
[header addSubview:cell];
// header的高度
header.height = self.topic.cellHeight + XMGTopicCellMargin;
// 設置header
self.tableView.tableHeaderView = header;
}
關于UIView的autoresizingMask屬性
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
self.contentView.backgroundColor = XMGGlobalBg;
// 創(chuàng)建label
UILabel *label = [[UILabel alloc] init];
label.textColor = XMGRGBColor(67, 67, 67);
label.width = 200;
label.x = XMGTopicCellMargin;
label.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:label];
self.label = label;
}
return self;
}
UIViewAutoresizingNone 不自動調(diào)整。
UIViewAutoresizingFlexibleLeftMargin 自動調(diào)整與superView左邊的距離,保證與superView右邊的距離不變。
UIViewAutoresizingFlexibleRightMargin 自動調(diào)整與superView的右邊距離,保證與superView左邊的距離不變。
UIViewAutoresizingFlexibleTopMargin 自動調(diào)整與superView頂部的距離,保證與superView底部的距離不變。
UIViewAutoresizingFlexibleBottomMargin 自動調(diào)整與superView底部的距離,也就是說,與superView頂部的距離不變。
UIViewAutoresizingFlexibleWidth 自動調(diào)整自己的寬度,保證與superView左邊和右邊的距離不變。
UIViewAutoresizingFlexibleHeight 自動調(diào)整自己的高度,保證與superView頂部和底部的距離不變。
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin 自動調(diào)整與superView左邊的距離,保證與左邊的距離和右邊的距離和原來距左邊和右邊的距離的比例不變。
MJExtension的映射
+ (NSDictionary *)replacedKeyFromPropertyName
{
return @{
@"small_image" : @"image0",
@"large_image" : @"image1",
@"middle_image" : @"image2",
@"ID" : @"id",
@"top_cmt" : @"top_cmt[0]" //,
// @"qzone_uid" : @"top_cmt[0].user.qzone_uid"
};
}
cell自動計算高度
IOS8的新特性“self-sizing”。
1. 要求cell.contentView的四條邊都與內(nèi)部元素有約束關系。
2. 添加這兩行代碼
self.tableView.estimatedRowHeight = 44.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
補充:如果cell.contentView里面還有其他內(nèi)容代替顯示自動伸縮的label,比如一個按鈕,且按鈕高度為22,那么再給自動伸縮的label添加一個約束label.height Greater than or equal 22,此時cell就可正常顯示。
AFNetworking取消任務
根據(jù)GET或POST的返回結果NSURLSessionDataTask執(zhí)行cancel,執(zhí)行后會回調(diào)失敗block,也可以這么寫
[self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
使用場景:
1.連續(xù)執(zhí)行多個請求,只管最后一個,比如:上拉刷新后沒請求回來就下拉刷新。
2.返回上個界面取消當前界面的所有請求,在dealloc方法里執(zhí)行取消。
點擊狀態(tài)欄回到頂部
.h文件
#import <Foundation/Foundation.h>
@interface XMGTopWindow : NSObject
+ (void)show;
+ (void)hide;
@end
.m文件
#import "XMGTopWindow.h"
@implementation XMGTopWindow
static UIWindow *window_;
+ (void)initialize
{
window_ = [[UIWindow alloc] init];
window_.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);
window_.windowLevel = UIWindowLevelAlert;
[window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClick)]];
}
+ (void)show
{
window_.hidden = NO;
}
+ (void)hide
{
window_.hidden = YES;
}
/**
* 監(jiān)聽窗口點擊
*/
+ (void)windowClick
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self searchScrollViewInView:window];
}
+ (void)searchScrollViewInView:(UIView *)superview
{
for (UIScrollView *subview in superview.subviews) {
// 主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// 以主窗口左上角為坐標原點, 計算subview的矩形框
CGRect newFrame = [keyWindow convertRect:subview.frame fromView:subview.superview];
CGRect winBounds = keyWindow.bounds;
// 主窗口的bounds 和 subview的矩形框 是否有重疊
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
BOOL isShowingOnKeyWindow = !subview.isHidden && subview.alpha > 0.01 && subview.window == keyWindow && intersects;
// 如果是scrollview, 滾動最頂部
if ([subview isKindOfClass:[UIScrollView class]] && isShowingOnKeyWindow) {
CGPoint offset = subview.contentOffset;
offset.y = - subview.contentInset.top;
[subview setContentOffset:offset animated:YES];
}
// 繼續(xù)查找子控件
[self searchScrollViewInView:subview];
}
}
@end
注意:上面的代碼會導致控制器中設置狀態(tài)欄樣式的代碼失效,如下代碼
/**
* 讓當前控制器對應的狀態(tài)欄是白色
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
解決方案:
1.info.plist中添加View controller-based status bar appearance,設置為NO;
2.使用UIApplication設置:[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
注意:因為是全局設置在返回的時候要恢復原來的樣式
監(jiān)聽tabbar的點擊
AppDelegate.h
@interface AppDelegate () <UITabBarControllerDelegate>
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
XMGTabBarController *tabBarController = [[XMGTabBarController alloc] init];
tabBarController.delegate = self;
self.window.rootViewController = tabBarController;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
// 發(fā)出一個通知
[XMGNoteCenter postNotificationName:XMGTabBarDidSelectNotification object:nil userInfo:nil];
}
給UIView設置背景圖片
// 設置背景圖片
- (void)drawRect:(CGRect)rect
{
[[UIImage imageNamed:@"mainCellBackground"] drawInRect:rect];
}
如果想重復調(diào)用drawRect,調(diào)用[self setNeedsDisplay];
計算總行數(shù)
// 總行數(shù) == (總個數(shù) + 每行最大數(shù) - 1) / 每行最大數(shù)
NSUInteger rows = (sqaures.count + maxCols - 1) / maxCols;
開發(fā)技巧
在xib中如果添加比如UIWebView、UIScrollView、UITableView等控件被導航欄擋住,就將這些控件放在最頂部。
導航欄enable顏色問題
如果設置了UINavigationBar的appearance改變了導航欄按鈕顏色,那么導航欄按鈕的enabled=NO灰色就會失效,需要手動設置狀態(tài)disable為灰色。此時如果在控制器的viewDidLoad里設置導航欄按鈕enabled為NO,不會生效,在viewDidAppear里才會生效。若想在viewDidLoad里生效,需要強制刷新。
- (void)viewDidLoad {
self.navigationItem.rightBarButtonItem.enabled = NO;
// 強制刷新
[self.navigationController.navigationBar layoutIfNeeded];
}
繼承UIScrollView的view
任何繼承自UIScrollView的view在添加到控制器后會自動增加64的高度。注意一定要第一個添加。如果不想讓系統(tǒng)自動增加,寫下面代碼
self.automaticallyAdjustsScrollViewInsets = NO;
setNeedsDisplay和setNeedsLayout
* setNeedsDisplay方法 : 會在恰當?shù)臅r刻自動調(diào)用drawRect:方法
* setNeedsLayout方法 : 會在恰當?shù)臅r刻調(diào)用layoutSubviews方法
presentedViewController
[a presentViewController:b animated:YES completion:nil];
a.presentedViewController -> b
b.presentingViewController -> a
判斷輸入框是否有文字
UITextField/UITextView都可以使用hasText方法判斷。
監(jiān)聽鍵盤上的?
繼承自UITextField重寫deleteBackward
- (void)deleteBackward {
!self.deleteBlock ? : self.deleteBlock();
[super deleteBackward];
}
快速取出數(shù)組中控件的屬性值
vc.tags = [self.tagLabels valueForKeyPath:@"text"];
子控件布局問題
自定義view子控件的布局代碼應該放在:
- (void)layoutSubviews {
[super layoutSubviews];
}
// 如需重新布局子控件調(diào)用
[self setNeedsLayout];
控制器中子控件的布局代碼也應該放在:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
鍵盤問題
如果回到上個頁面鍵盤不發(fā)出通知,就先退出鍵盤,在啟用即可
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 先退出之前的鍵盤
[self.view endEditing:YES];
// 再叫出鍵盤
[self.textView becomeFirstResponder];
}
自定義導航欄返回按鈕失效
在自定義導航控制器中添加下面代碼即可
- (void)viewDidLoad {
[super viewDidLoad];
// 如果滑動移除控制器的功能失效,清空代理(讓導航控制器重新設置這個功能)
self.interactivePopGestureRecognizer.delegate = nil;
}
imageMode

UITabBar,移除頂部的陰影
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
NSDictionary 轉(zhuǎn) NSString
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parametersDic options:0 error:nil];
NSString *requestBody = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
設置Status bar顏色
如果沒有navigation bar, 直接設置
self.view.backgroundColor = [UIColor lightGrayColor];
如果有navigation bar, 在navigation bar 添加一個view來設置顏色。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];
[view setBackgroundColor:[UIColor lightGrayColor]];
[viewController.navigationController.navigationBar addSubview:view];
使用AirDrop 進行分享:
NSArray *array = @[@"test1", @"test2"];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:^{
NSLog(@"Air");
}];
給UIView 設置透明度,不影響其他sub views
設置background color的顏色中的透明度
[self.testView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
//或
[self.testView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
給scrollview設置keyboardDismissMode實現(xiàn)Message app里面的隱藏鍵盤功能

instrument不能調(diào)試iOS8設備
解決:啟用UI Automation,設置 -> 開發(fā)者 -> Enable UI Automation
http://stackoverflow.com/questions/24428197/xcode-6-with-ios-8-sdk-cant-run-ui-automation-in-instruments
崩潰:breakpoint 1.1
這個是程序運行的時候進斷點了,把斷點去掉就可以了
歸檔和反歸檔:
//創(chuàng)建
- (void)createSingleton {
//獲得Document的路徑
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pathString = [documents stringByAppendingPathComponent:path];//拓展名可以自己隨便取
[NSKeyedArchiver archiveRootObject:self toFile:pathString];
}
//讀取
- (CommonSingleton *)readSingleton {
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pathString = [documents stringByAppendingPathComponent:path];
CommonSingleton *singleton = [NSKeyedUnarchiver unarchiveObjectWithFile:pathString];
return singleton;
}
Cornerstone下載地址
Cornerstone3.0.3:http://soft.macx.cn/soft3947.htm
Cornerstone3.0.2:
http://bbs.feng.com/forum.php?mod=viewthread&tid=10810280&mobile=no
Cornerstone3.0:http://www.xp510.com/mac/30966.html
Cornerstone2.7.17:http://www.chinamac.com/download/mac53486.html
Cornerstone2.7.14:http://www.maczapp.com/cornerstone
Cornerstone2.7.10:http://bbs.feng.com/read-htm-tid-7936664.html
iOS項目代碼行數(shù)統(tǒng)計
1、打開終端
2、cd 進入項目根目錄
3、輸入命令find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.h" -or -name "*.rss" ")" -print | xargs wc -l
4、回車
精度計算原則
先轉(zhuǎn)換為int計算,計算完后在轉(zhuǎn)換為float,此時比較準確
Xcode8注釋插件
(VVDocumenter)快捷鍵:option + command + /
Xcode8運行一堆沒用的logs解決辦法
設置OS_ACTIVITY_MODE : disable,如下圖:

Xcode dmg文件下載地址
https://developer.apple.com/download/more/
多用塊枚舉,少用for循環(huán)
NSDictionary *aDictionary = /* ... */;
[aDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop){
// Do something with 'key' and 'obj'
}];
這樣做的好處是:在使用塊進行快速枚舉的時候,我們可以不創(chuàng)建臨時數(shù)組。雖然語法上沒有快速枚舉簡潔,但是我們可以獲得數(shù)組元素對應的序號,字典元素對應的鍵值,而且,我們還可以隨時令遍歷終止。如果我們可以知道集合里的元素類型,就可以修改簽名。這樣做的好處是:可以讓編譯期檢查該元素是否可以實現(xiàn)我們想調(diào)用的方法,如果不能實現(xiàn),就做另外的處理。這樣一來,程序就能變得更加安全。
訪問實例變量注意
1.初始化方法和dealloc方法中,需要直接訪問實例變量來進行設置屬性操作。因為如果在這里沒有繞過set方法,就有可能觸發(fā)其他不必要的操作。
2.惰性初始化(lazy initialization)的屬性,必須通過屬性來讀取數(shù)據(jù)。因為惰性初始化是通過重寫get方法來初始化實例變量的,如果不通過屬性來讀取該實例變量,那么這個實例變量就永遠不會被初始化。
支持http請求:
1 在Info.plist中添加NSAppTransportSecurity類型Dictionary。
2 在NSAppTransportSecurity下添加NSAllowsArbitraryLoads類型Boolean,值設為YES
不過從2017年1月1日起,,所有新提交的 app 默認不允許使用NSAllowsArbitraryLoads來繞過ATS的限制,默認情況下你的 app 可以訪問加密足夠強的(TLS V1.2以上)HTTPS內(nèi)容;
3.可以選擇使用NSExceptionDomains設置白名單的方式對特定的域名開放HTTP內(nèi)容來通過審核,比如說你的應用集成了第三方的登錄分享SDK,可以通過這種方式來做,下面以新浪SDK作為示范(Source Code 模式下):
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>sina.cn</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
全能初始化方法
有時,由于要實現(xiàn)各種設計需求,一個類可以有多個創(chuàng)建實例的初始化方法。我們應該選定其中一個作為全能初始化方法,令其他初始化方法都來調(diào)用它。
AFN回調(diào)注意
AFN的成功失敗回調(diào)代碼已經(jīng)在主線程了,所以不需要在代碼塊里寫放在主線程中執(zhí)行的代碼。
AFN的成功失敗代碼塊不需要使用weakSelf,因為造成循環(huán)引用的前提條件是控制器和代碼塊互相擁有,這里的AFN代碼塊不被控制器擁有,在控制器定義的block就會循環(huán)引用,這時候得使用weakSelf避免循環(huán)引用.
以“自動釋放池快”降低內(nèi)存峰值
內(nèi)存峰值(high-memory waterline)是指應用程序在某個限定時段內(nèi)的最大內(nèi)存用量(highest memory footprint)。新增的自動釋放池塊可以減少這個峰值:
NSArray *databaseRecords = /* ... */;
NSMutableArray *people = [NSMutableArray new];
for (NSDictionary *record in databaseRecords) {
@autoreleasepool {
EOCPerson *person = [[EOCPerson alloc] initWithRecord:record];
[people addObject:person];
}
}
這樣一來,每次循環(huán)結束,我們都會將臨時對象放在這個池里面,而不是線程的主池里面。
插件路徑
Xcode8:~/Library/Developer/Xcode/Plug-ins
Xcode8以前:~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/
iPhone各屏幕尺寸
| 機型 | 像素 | 屏幕尺寸 |
|---|---|---|
| iPhone4 | 640 x 960 | 320 x 480 |
| iPhone5 | 640 x 1136 | 320 x 568 |
| iPhone6 | 750 x 1334 | 375 x 667 |
| iPhone6p | 1080x19201242 x 2208
|
414 x 736 |
有關屏幕的任何問題請查看鏈接:
http://blog.csdn.net/phunxm/article/details/42174937
推薦的開發(fā)筆記:
http://www.itdecent.cn/p/d333cf6ae4b0#
http://www.cnblogs.com/yangmx/p/5631457.html