1. tableView刷新閃屏
場景:類似于聊天界面界弧,快速多次發(fā)送消息临梗,刷新界面并且滾動到最后一條
問題:scrollToRowAtIndexPath:
滾動到最后時,會再次出現(xiàn)從上往下滾動的現(xiàn)象
原因:在tableview
滾動到最后一條前痊银,還未得出cell
的height
,就已經開始新的reloadData
解決:
tableView.estimatedRowHeight = 0
參考鏈接:
http://www.reibang.com/p/ef815c336ded
2. 打開webView
的同時 輸入框 獲取焦點并彈出鍵盤
問題:h5頁面已設置頁面中input
獲取焦點谣妻,安卓端展示正常鳞滨,但是iOS不能顯示光標和鍵盤???????
原因:在WebView
中, 默認是需要用戶操作行為才能打開鍵盤累盗,若需要打開頁面的同時輸入框獲取焦點并彈出鍵盤寒矿,需要設置keyboardDisplayRequiresUserAction
屬性告知webView
解決:
2.1 UIWebView
webView.keyboardDisplayRequiresUserAction = NO;
2.1 WKWebView
OC 語言:
@implementation WKWebView (Keyboard)
static void (*originalIMP)(id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) = NULL;
void interceptIMP (id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
originalIMP(self, _cmd, arg0, TRUE, arg2, arg3);
}
//該函數(shù)只能調用一次突琳,否則會導致循環(huán)調用若债,程序崩潰
+ (void)wkWebViewShowKeybord{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class cls = NSClassFromString(@"WKContentView");
SEL originalSelector = NSSelectorFromString(@"_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
IMP impOvverride = (IMP) interceptIMP;
originalIMP = (void *)method_getImplementation(originalMethod);
method_setImplementation(originalMethod, impOvverride);
});
}
@end
swift語言(extension中添加屬性):
import WebKit
typealias ClosureType = @convention(c) (Any, Selector, UnsafeRawPointer, Bool, Bool, Any) -> Void
extension WKWebView{
var keyboardDisplayRequiresUserAction: Bool {
get {
return true
} set {
if newValue == false {
setKeyboardRequiresUserInteraction()
}
}
}
func setKeyboardRequiresUserInteraction() {
let sel: Selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:")
let WKContentView: AnyClass = NSClassFromString("WKContentView")!
let method = class_getInstanceMethod(WKContentView, sel)
let originalImp: IMP = method_getImplementation(method!)
let original: ClosureType = unsafeBitCast(originalImp, to: ClosureType.self)
let block : @convention(block) (Any, UnsafeRawPointer, Bool, Bool, Any) -> Void = {(me, arg0, arg1, arg2, arg3) in
original(me, sel, arg0, true, arg2, arg3)
}
let imp: IMP = imp_implementationWithBlock(block)
method_setImplementation(method!, imp)
}
}
參考鏈接:https://blog.csdn.net/longshihua/article/details/78001336
3. UIAlertController
在pad上崩潰
問題:UIAlertController
的preferredStyle
為actionSheet
時在pad上crash,iphone上正常
原因:在iPad下ActionSheet
將以popover
的形式展現(xiàn)出來
解決:設置sourceView
和sourceRect
屬性
alertVC.popoverPresentationController?.sourceView = view
let sourceRect = CGRect.init(x: view.width / 2.0, y: view.height, width: 1, height: 1)
alertVC.popoverPresentationController?.sourceRect = sourceRect
4. 撥打電話openUrl
問題:調用撥打電話的方法openUrl
,在iOS10以上出現(xiàn)卡頓拆融,或阻塞其他彈窗的正常消失/出現(xiàn)
原因:在iOS10以上openUrl
被deprecated蠢琳,調用時卡了主線程
解決:區(qū)分iOS系統(tǒng),10以上和以下調用不同的方法
// 撥打電話
public class func callTelephone(phoneNum : String) -> Bool {
let urlStr = "telprompt://\(phoneNum)"
guard let url = URL.init(string: urlStr), UIApplication.shared.canOpenURL(url) else {
return false
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
return true
}
5. interactivePopGestureRecognizer 頁面卡死或無響應
問題:在navigationController
管理的第一個主頁面PageA
镜豹,使用“邊緣手勢-從左側向右滑動”傲须,點擊通過push
進入下一頁面PageB
,頁面卡死
原因:
解決:關閉主頁面PageA
的邊緣手勢
1)設置邊緣手勢是否開啟-當是主頁面時趟脂,關閉邊緣手勢
//設置泰讽,返回手勢是否可用
func setGestureEnable(enable: Bool) {
guard let navi = navigationController else { return }
if navi.viewControllers.count >= 2, enable {
navi.interactivePopGestureRecognizer?.isEnabled = true
navi.interactivePopGestureRecognizer?.delegate = self
} else {
navi.interactivePopGestureRecognizer?.isEnabled = false
navi.interactivePopGestureRecognizer?.delegate = nil
}
}
2)實現(xiàn)代理方法
extension UIViewController: UIGestureRecognizerDelegate {
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
3)調用方法 - 在viewDidAppear
方法中調用
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setGestureEnable(enable: true)
}
注:不能在viewWillAppear
中調用,否則在跳轉到PageB
頁面后昔期,使用手勢側滑返回時已卸,頁面會卡死