之前在做項目的時候經常會遇到一些細小的知識點,不影響做項目的大方向,但確影響了很多細小的點..比如公式轉換,漢字拼音,顏色轉換等等,什么都涉及...上次寫的還是在CSDN
寫blog時記錄的:iOS開發 容易犯錯的知識點和不錯的細小知識點(持續更新),現在換地方了,就不在接著原來的寫了,重開一個,依舊持續更新/記錄.
35.當你向項目中添加一個文件夾時,往往會面對Create groups
和Create folder references
的選擇
前者Create groups for any added folders : 給任一你添加的文件創建一個組groups
后者Create folder references for any added folders :給任一你添加的文件創建一個文件夾folder
兩的區別是:前者的文件夾是黃色的;后者的文件夾是藍色的
如果有一個info.h文件需要引用:前者直接導入import "info.h"就可能使用;后者你需要import "文件夾名字/info.h"才可能使用,否則編譯時找不到文件info.h。
36.Pointer is missing a nullability type specifier (__nonnull or __nullable)的解決方法:
首尾加入NS_ASSUME_NONNULL_BEGIN
和NS_ASSUME_NONNULL_END
37.UIButton文本對齊問題
這里使用button.titleLabel.textAlignment = NSTextAlignmentLeft;
這行代碼是沒有效果的,這只是讓標簽中的文本左對齊,但并沒有改變標簽在按鈕中的對齊方式.
所以,我們首先要使用button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
這行代碼,把按鈕的內容(控件)的對齊方式修改為水平左對齊,但是這們會緊緊靠著左邊,不好看...
所以我們還可以修改屬性:button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
這行代碼可以讓按鈕的內容(控件)距離左邊10個像素,這樣就OK了
38.獲取window的方法之前都是直接[UIApplication sharedApplication].keyWindow;
之后導入bug tags的時候提醒用比較嚴謹的獲取方法
- (UIWindow *)lastWindow
{
NSArray *windows = [UIApplication sharedApplication].windows;
for(UIWindow *window in [windows reverseObjectEnumerator]) {
if ([window isKindOfClass:[UIWindow class]] &&
CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
return window;
}
return [UIApplication sharedApplication].keyWindow;
}
39.延長LaunchImage的顯示時間
如果你覺得你開啟太快,那么漂亮得LaunchImage還沒怎么展示就跳過了.你可以在你的第一個加載頁面中添加如下代碼來
NSThread.sleepForTimeInterval(3.0)//延長3秒
40.關閉鍵盤的自動聯想和首字母大寫功能
[_userNameTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
[_userNameTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
41.設置導航欄下方不顯示內容(此時導航欄無透明度)
self.extendedLayoutIncludesOpaqueBars = YES;
42.監測鍵盤通知時,發送多次的解決情況
typedef NS_ENUM(NSInteger, KeyBoardAction) {
KeyBoardActionShow = 0,
KeyBoardActionHide = 1
}
self.action = 0;
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hide) name:UIKeyboardWillHideNotification object:nil];
- (void)show {
// 防止按home鍵退出后臺再返回前臺發送的多余通知
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
return;
}
if (_action == KeyBoardActionShow) {
NSLog(@" name = %s",__FUNCTION__);
_action = KeyBoardActionHide;
}
}
- (void)hide {
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
return;
}
if (_action == KeyBoardActionHide) {
NSLog(@" name = %s",__FUNCTION__);
_action = KeyBoardActionShow;
}
}
43.http/https請求獲取cookie的問題
項目需要獲取cookie,但是網上找的獲取cookie的方法:
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
NSLog(@"%@", cookie);
}
無法獲取完整的cookie,打印出來的只有一小部分....實際獲取的方法是:
NSString *cookieString = [[request.requestOperation.response allHeaderFields ] valueForKey:@"Set-Cookie"];
44.金融產品的金額形式變化
// 30000000 - > 30,000,000
- (NSString *)addMarkToString {
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: self.intValue]];
return numberString;
}
+(NSString *)countNumAndChangeformat:(CGFloat)num
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init] ;
[numberFormatter setPositiveFormat:@"###,##0.00;"];
NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:num]];
return formattedNumberString;
}
45.iOS中使用blend改變圖片顏色
// UIImage Category
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode
{
//We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
//Draw the tinted image in context
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn) {
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
}
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
喵神有篇文章講這個的->iOS中使用blend改變圖片顏色
46.tableView的分割線左邊不到頭的問題
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
}
47.實現UITableView Plain SectionView和table不停留一起滑動
1) 這個代碼是通過scroll偏移量來監聽和改變你的tableview的contentInset 可見很不好(試試就知道) X
// 去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 40;(你的section高度)
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
}
else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
2)和第一個沒太大本質區別,在自定義headSectionView中重寫setframe方法來重載table的sectionTablePlainSectionView
- (void)setFrame:(CGRect)frame{
CGRect sectionRect = [self.tableView rectForSection:self.section];
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame)); [super setFrame:newFrame];
}
3)不推薦使用Plain style,改用 Group style, 只需要將 footerSectionHeight 設置為0.01(不是固定的,保證很小就行), HeaderSectionHeight按照實際需求高度多少設置就行
,一樣可以實現 Plain style 的效果
48.Swift 沙盒路徑集合
// MARK: - 沙盒路徑集合
// http://stackoverflow.com/questions/32501627/stringbyappendingpathcomponent-is-unavailable
// http://stackoverflow.com/questions/32120581/stringbyappendingpathcomponent-is-unavailable
extension String {
var lastPathComponent: String {
return (self as NSString).lastPathComponent
}
var pathExtension: String {
return (self as NSString).pathExtension
}
var stringByDeletingLastPathComponent: String {
return (self as NSString).stringByDeletingLastPathComponent
}
var stringByDeletingPathExtension: String {
return (self as NSString).stringByDeletingPathExtension
}
var pathComponents: [String] {
return (self as NSString).pathComponents
}
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.stringByAppendingPathComponent(path)
}
func stringByAppendingPathExtension(ext: String) -> String? {
let nsSt = self as NSString
return nsSt.stringByAppendingPathExtension(ext)
}
}
49.用hidesBottomBarWhenPushed屬性實現隱藏BottomBar時候的的幾個坑!
在創建好后,馬上調用 vc.hidesBottomBarWhenPushed = true
下面有幾個新手常掉進去的坑:
1.不能夠在viewDidLoad() 方法里調用, 一定要在viewDidLoad加載之前 也就是 push之前調用:
2.區分下面兩個方法
A -> hidesBottomBarWhenPushed = true
B -> self.navigationController?.hidesBottomBarWhenPushed = true 這個方法會因為沒有被push前,就沒有navigationController, 所以設置無效
50.UIViewcontroller的title
// will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.
self.title = @"Your title";
// will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.
self.navigationItem.title = @"Your title";
// will set the title of a UITabBarItem but NOT the UINavigationBar.
self.navigationController.title = @"Your title";
51.memory-leak-when-using-wkscriptmessagehandler
http://stackoverflow.com/questions/31094110/memory-leak-when-using-wkscriptmessagehandler
52. UIScrollView 的 autolayout
Storyboard中,如果父視圖是 UIScrollView, 在它上面不要直接繪制各個子控件, 應該先拖一個UIView約束為(0,0,0,0),再到這個 view 上去繪制.