RobertZh
1/31/2018 - 2:10 AM

iOS

各种坑

1. 一般对于webView都会有根据固宽计算实际内容高度、图片内容居中、防内容拷贝
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    webView.scrollView.scrollEnabled = NO;
    // 根据固定宽度获取高度
    NSString *jSString = [NSString stringWithFormat:@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=%f'); document.getElementsByTagName('head')[0].appendChild(meta);", SCREEN_WIDTH];
    [webView stringByEvaluatingJavaScriptFromString:jSString];
    // 内容图片居中
    NSString *js=@"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"function ResizeImages() { "
    "var myimg,oldwidth;"
    "var maxwidth = %f;"
    "for(i=0;i <document.images.length;i++){"
    "myimg = document.images[i];"
    "if(myimg.width > maxwidth){"
    "oldwidth = myimg.width;"
    "myimg.width = %f;"
    "}"
    "}"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    js=[NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,webView.bounds.size.width];
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    // 防内容拷贝
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none'"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none'"];
    
    CGRect frame = webView.frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    self.webViewHeight.constant = fittingSize.height;
}
1. 有时候,一个tableView的cell不仅是根据实际内容计算高度(cell动态高度,可以用iOS8后的estimatedHeight),还要根据这些动态cell来计算tableView的内容高度。
    网上利用[tableView layoutIfNeeded]; tableViewHeight = tableView.contentSize.height; 大部分情形适应,也有因为tableView的高度计算不准备而导致页面显示不全问题
    这里个人的解决方案是:获取最后一个tableViewCell的实际frame来计算高度(CGRect lastCellRect = [tableView rectForRowAtIndexPath:indexPath];tableViewHeight = CGRectGetMaxY(lastCellRect);)
1. 通过工具定位到卡顿范围
2. 找到原因,寻找解决方案
3. 卡顿原因归纳分类
1. 后台返回的HTML转换成NSString,尽量不要使用下面方法,耗性能,如果这个转换是放在cellForRow + 子线程中,可能会闪退
  + (NSMutableAttributedString *)praseHtmlStr:(NSString *)htmlStr {
      NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
      [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, attributedString.length)];
      [attributedString addAttribute:NSForegroundColorAttributeName value:CommonColor(Color333333) range:NSMakeRange(0, attributedString.length)];
      return attributedString;
  }
  建议过滤掉HTML中的标签,下面是用正则过滤
    + (NSMutableAttributedString *)parseHtmlStr:(NSString *)htmlStr {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, attributedString.length)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:CommonColor(Color333333) range:NSMakeRange(0, attributedString.length)];
        return attributedString;
    }
    (这哥们儿总结的不错:https://www.jianshu.com/p/8973f5439cc0)
1. iOS8.1下,UIImageView会出现图片的拉伸或压缩问题,可以将该图片资源重新放到Bundle下,不要放在Assets里。
1. 上下滑动UIScrollView隐藏/显示导航栏。简书上这位作者很好地解决了这段代码。(https://www.jianshu.com/p/b43113256ce1)
  情景复现:但是当前页面在隐藏导航栏状态下,按下Home键,再次进入APP,上下滑动时导航栏底部会有黑线上下移动。
  解决方案:在当前控制器的viewDidLoad方法里添加App退出时的通知监听(UIApplicationWillResignActiveNotification),
            一旦监听到通知后,手动将导航栏显示出来。([self.navigationController setNavigationBarHidden:NO animated:YES];)

2. iOS11,在导航上设置自定义的按钮在UIBarButtonItem上时,设定按钮的frame大小无效,需要[view addSubview:button] && [[UIBarButtonItem alloc] initWithCustomView:view]或者用系统的[[UIBarButtonItem alloc] initWithImage:<#(nullable UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>]