iOS 開發小技巧

UI

自定義了leftBarbuttonItem左滑返回手勢失效了怎麼辦?

方式一

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                         initWithImage:img
                                         style:UIBarButtonItemStylePlain
                                         target:self
                                         action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;

方式二

在編輯 Tableview 的過程中 (如 tableview 正在執行刪除 cell 的動畫),這個時候返回 NavigationController 的上一級,就會導致左劃返回手勢失敗。這個時候可以在 動畫完成後再返回或者動畫過程中不允許返回

[CATransaction begin];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[_deleteIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
[CATransaction setCompletionBlock: ^{
    [self.navigationController.navigationBar setUserInteractionEnabled:YES];
}];
[self.tableView endUpdates];
[CATransaction commit];

ScrollView莫名其妙不能在viewController劃到頂怎麼辦?

self.automaticallyAdjustsScrollViewInsets = NO;

鍵盤事件寫的好煩躁,都想摔鍵盤了,怎麼辦?

1.買個結實的鍵盤.

2.使用IQKeyboardManager(github上可搜索),用完之後腰也不疼了,腿也不酸了.

怎麼在不新建一個Cell的情況下調整separaLine的位置?

_myTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);

怎麼像safari一樣滑動的時候隱藏navigationbar?

navigationController.hidesBarsOnSwipe = YES;

導航條返回鍵帶的title太討厭了,怎麼讓它消失!

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                forBarMetrics:UIBarMetricsDefault];

CollectionView 怎麼實現tableview那種懸停的header?

CSStickyHeaderFlowLayou

Cell 自適應高度

要讓 table view 的 cell 自適應內容,有幾個要點:

1.設置的 AutoLayout 約束必須讓 cell 的 contentView 知道如何自動延展。關鍵點是 contentView 的 4 個邊都要設置連接到內容的約束,並且內容是會動態改變尺寸的。
2.UITableView 的 rowHeight 的值要設置爲 UITableViewAutomaticDimension
3.和 iOS 7 一樣,可以實現 estimatedHeightForRowAtIndexPath 方法提升 table view 的第一次加載速度。
4.任何時候 cell 的 intrinsicContentSize 改變了(比如 table view 的寬度變了),都必須重新加載 table view 以更新 cell。

在 collection view 中也能讓 cell 自適應內容大小

在 collection view 中也能讓 cell 自適應內容大小,如果 UICollectionView 的 layout 是一個 UICollectionViewFlowLayout,只需要將 layout.itemSize = … 改成 layout.estimatedItemSize = …。 只要設置了 layout 的 estimatedItemSize,collection view 就會根據 cell 裏面的 autolayout 約束去確定cell 的大小。

原理:

1.collection view 根據 layout 的 estimatedItemSize 算出估計的 contentSize,有了 contentSize collection view 就開始顯示
2.collection view 在顯示的過程中,即將被顯示的 cell 根據 autolayout 的約束算出自適應內容的 size
3.layout 從 collection view 裏獲取更新過的 size attribute
4.layout 返回最終的 size attribute 給 collection view
5.collection 使用這個最終的 size attribute 展示 cell

怎麼播放GIF的時候這麼卡,有沒有好點的庫?

FlipBoard出品的太適合你了
https://github.com/Flipboard/FLAnimatedImage
YLGIFImage
https://github.com/liyong03/YLGIFImage

怎麼把tableview裏cell的小對勾的顏色改成別的顏色?

_mTableView.tintColor = [UIColor redColor];

本來我的statusbar是lightcontent的,結果用UIImagePickerController會導致我的statusbar的樣式變成黑色,怎麼辦?

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

怎麼把我的navigationbar弄成透明的而不是帶模糊的效果?

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

怎麼改變uitextfield placeholder的顏色和位置?

// 繼承uitextfield,重寫這個方法
- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}

怎樣在 UITextView 中顯示 HTML 格式的文本

NSString *htmlString = @"<p><strong>自定義了leftBarbuttonItem左滑返回手勢失效了怎麼辦?</strong></p>";
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.result.attributedText = attributedString;

這裏寫圖片描述

字符串全角和半角轉換

NSString *string = @"hehe"; 

NSMutableString *convertedString = [string mutableCopy]; 

// 半角轉全角只需要把 kCFStringTransformFullwidthHalfwidth 換成kCFStringTransformHiraganaKatakana 即可。
CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformFullwidthHalfwidth, false); 

NSLong(@"ddc:%@",convertString);

ipa 解包後看不到 png 資源怎麼辦

當 Xcode 打包時會使用自帶的圖片壓縮工具 pngcrush 壓縮 png 圖片。如果解包後發現部分 png 圖片不能查看,可以使用下面的命令來解壓縮圖片
pngcrush -revert-iphone-optimizations src des
下面是唐巧大神在《iOS 開發進階》中提供的一段腳本

#!/bin/bash

mkdir OriginImages

for png in `find . -name '*.png'`
do
    name=`basename $png`
    pngcrush -revert-iphone-optimizations $name OriginImages/$name
done

Objective-C

對象下標(像數組或字典一樣訪問對象數據)

參考文章

- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;

在頭文件聲明上面兩個方法,並在 .m 中實現,就可以實現類似 C 語言數組訪問數據的方式來訪問自定義對象的數據
如:

SubscriptTestObject *object = [[SubscriptTestObject alloc] init];    
id aa = object[10];
object[11] = @"12";

在頭文件聲明下面兩個方法,並在 .m 中實現,就可以實現 dic[@"key"] = @"value" 方式來訪問和修改自定義對象的數據

- (id)objectForKeyedSubscript:(id <NSCopying>)key;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;

如:

SubscriptTestObject *object = [[SubscriptTestObject alloc] init];
id aa = object[@"key"];
object[@"key01"] = @"12345";
[object setObject:@"value" forKeyedSubscript:@"key02"];

參考文章

《iOS開發的一些奇巧淫技》

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章