以下小技巧都是本人在開發(fā)中得出的小經驗,如有錯誤歡迎指教,有更好的技巧歡迎留言交流常侦,以下如有雷同浇冰,純屬巧合,謝謝聋亡!
1.給新建工程中的storyboard添加navigationbar
2.在Nib中選中UIImageView之后肘习,勾選Clip Subviews將會裁剪掉超出UIImageView的范圍外的內容
3.開發(fā)中的編碼與解碼
- 1>上傳漢字到服務器,服務器那邊要是沒有做編碼的話坡倔,漢字是亂碼漂佩,此時需要由程序員轉碼成UTF-8,用到的方法是:
NSString * encodingString = [@"你需要上傳轉碼的字符串" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- 2>通過接口請求后罪塔,返回的數(shù)據投蝉,格式顯示如為:%3A%2F%2F樣式,此時需要我們進行UTF-8解碼征堪,用到的方法是:
NSString *nameString = [model.album_name(你從服務器請求下來的數(shù)據瘩缆,如名字) stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
4.弧度與角度互轉(宏)
// 弧度轉角度
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
// 角度轉弧度
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
4.判斷控件是否在某個試圖上
if ([_label isDescendantOfView: self.testView]) {
NSLog(@"在視圖上");
}
5.設置圖片不被拉伸
6.設置圖片拉伸方式
7.Nib中設置Aspect Fit之后設置高度,圖片會動設置寬高比
(如:圖片原本size為 100 x 45佃蚜,但你使用的時候感覺寬度太窄想要200庸娱,此時就可進行設置,當設置寬度為200時谐算,高度自動跟隨寬度設置)
8.通知系統(tǒng)涌韩,馬上更新布局([self.view layoutIfNeeded])
9.文件夾改為.bundle包
10.在桌面創(chuàng)建一個.plist文件,方便查看請求數(shù)據(在AF請求成功的Block中寫)
// responseObject為AF請求下來的數(shù)據 參數(shù)一:你隨便從你電腦桌面上拉取一個東西放此處就能得到路徑,然后起個你喜歡的名字后綴為.plist
[responseObject writeToFile:@"/Users/xxxxxxx/Desktop/xxxx.plist" atomically:YES];
11.ARC工程更改為MRC
目前都是自動內存管理氯夷,但想深入了解引用計數(shù)器的+1 臣樱、 -1 、釋放回收的機制腮考,可以一試
12.使用cocoapods中...Podfile like so報錯
13.debug模式的運用
- 定義debug宏(測試):
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
-
關閉debug宏(發(fā)布):
快照.png
快照2.png
14.iOS launch啟動界面全屏
1.在<APP>-info.list文件中雇毫,加上“Status bar is initially hidden”選項,選擇yes
2在程序里面添加 [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
即可
15.layout踩蔚、drawRect的調用
[self setNeedsLayout] 會在恰當?shù)臅r候調用 layoutSubviews 方法
[self setNeedsDisplay] 會在恰當?shù)臅r候調用 drawRect 方法
16.Nib按鈕設置內容偏移
Edge 選擇你要移動的內容棚放,可以是文字、圖片馅闽、或者是整體
17.Nib中按鈕設置圓角
18.Xib中l(wèi)abel如何換行飘蚯?
在nib或者SB中,一個labe要求顯示多行的時候福也,在換行的地方使用 option + enter 即可實現(xiàn)換行
19.自動縮放到圖片原本尺寸大芯种琛:command 加 =
使用之前
使用之后
20.swift3.0延遲執(zhí)行代碼
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3.0) {
print(要執(zhí)行的代碼)
}
21.貝塞爾曲線角度參考圖
22.自定義代碼片段
@property (nonatomic, retain) <#type#> *<#name#>
※號代表一個空格
代碼片段的路徑
23.CAShapeLayer屬性圖
24.Button防止短時間內重復點擊
25.計算最大行數(shù)
// 總行數(shù) = (總個數(shù) + 每行的最大數(shù) - 1) / 每行最大數(shù)
NSUInteger rows = (dataArr.count + maxCols - 1) / maxCols;
26.修改項目名及腳本替換類名
https://blog.csdn.net/qq_23292307/article/details/80915654
27.修改SearchBar占位文字顏色及大小
UISearchBar *serchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(58, 10, SCREEN_W-58*2, 30)];
serchBar.placeholder = @"輸入癥狀、疾病暴凑、醫(yī)生峦甩、科室、醫(yī)院";
// 修改占位文字
UITextField * searchField = [serchBar valueForKey:@"_searchField"];
[searchField setValue:Font_Shallow_999999 forKeyPath:@"_placeholderLabel.textColor"];
[searchField setValue:[UIFont boldSystemFontOfSize:11] forKeyPath:@"_placeholderLabel.font"];
serchBar.barTintColor = [UIColor whiteColor];
serchBar.layer.borderColor = Line_e0e0e0.CGColor;
serchBar.layer.borderWidth = 1;
serchBar.layer.masksToBounds = YES;
serchBar.layer.cornerRadius = 15;
[upView addSubview:serchBar];
###兼容iOS13(iOS13使用kvc獲取會奔潰)
UISearchBar *serchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(58, 10, SCREEN_W-58*2, 30)];
serchBar.placeholder = @"輸入癥狀现喳、疾病凯傲、醫(yī)生犬辰、科室、醫(yī)院";
serchBar.barTintColor = [UIColor whiteColor];
serchBar.layer.borderColor = Line_e0e0e0.CGColor;
serchBar.layer.borderWidth = 1;
serchBar.layer.masksToBounds = YES;
serchBar.layer.cornerRadius = 15;
[self addSubview:serchBar];
UITextField * searchField = nil;
if (@available(iOS 13.0, *)) {
searchField = serchBar.searchTextField;
}else {
searchField = [serchBar valueForKey:@"_searchField"];
}
NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc] initWithString:searchField.placeholder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:Font_Shallow_999999}];
searchField.attributedPlaceholder = arrStr;
28.從數(shù)組中刪除冰单,防止重復添加
// 從父類中抹掉之前創(chuàng)建標簽,防止重復添加
[self.labelArr makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.labelArr removeAllObjects];
29.返回到指定頁面
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[ChatViewController class]]) {
[self.navigationController popToViewController:controller animated:YES];
}
}
30.A(帶Nav) present B(B不需要Nav)幌缝, B push C(需要Nav) A->B ->C
1、在A中Push B時用Nav包裝B诫欠,present時使用nav
let nav = NavgationViewController.init(rootViewController: B_ViewController())
self.present(nav, animated: true, completion: nil)
2涵卵、在B中設置加載隱藏Nav,結束顯示Nav
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
3呕诉、B正常push C即可
let vc = C_ViewController()
self?.navigationController?.pushViewController(vc, animated: true)
注:如B可push到C缘厢,但C中不顯示Nav吃度,設置C顯示時不隱藏Nav
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
最后附帶一些常用的快捷鍵
名稱 | 快捷鍵 注:0 為數(shù)字/ O為字母 |
---|---|
顯示/隱藏 左側 工具面板 | Command + 0 |
顯示/隱藏 右側 工具面板 | Command + Option + 0 |
Nib或者SB中快速打開關聯(lián)類 | Command + Option + enter |
方法查找 | Control + 6 |
快速打開查找 | Command + Shift + O |
文檔和參考 | Command + Shift + 0 |
收起/展開方法實現(xiàn) | Command + Option + ?? / ?? |
返回/前進上次打開的界面 | Command + Control + ?? / ?? |
打開 | Command + O |
關閉窗口 | Command + W |
上/下移動代碼 | Command + Option + [ / ] |