iOS 13 适配总结

自从6月份的 WWDC大会展示了iOS 13的新版本之后,广大开发者朋友又面临着新一轮的系统升级适配工作;随着苹果9月份发布会脚步的临近,对公司的App升级适配势在必行。

iOS 13发现问题回顾

  • 禁止用户获取或直接设置私有属性:调用setValue:forKeyPath:valueForKey:方法会引起App崩溃。例如:UITextField修改_placeholderLabel.textColorUISearchBar修改_searchField
  • UITextFieldleftViewrightView调整:部分视图位置显示异常
  • UITabBar部分调整:UITabBarItem播放gif显示比例有问题;UITabBarItem只显示图片时,图片位置偏移;Badge文字显示偏大
  • UITableViewcell选中样式失效
  • 第三方SDK的闪退兼容问题
将所有问题归纳总结,得出以下几点解决方案的建议和示例代码,记录一下

1. UITextField

  • 设置placeholder引起的闪退

    iOS 13之前,设置placeholder有三种方案:

    • 基于KVO简单粗暴的修改私有属性(iOS 13禁用)

      [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
      [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
      
    • 设置attributedPlaceholder属性

      textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"placeholder" attributes:@{
             NSForegroundColorAttributeName: [UIColor darkGrayColor], NSFontAttributeName: [UIFont systemFontOfSize:13]}];
      
    • 重写UITextField子类的drawPlaceholderInRect:方法

      - (void)drawPlaceholderInRect:(CGRect)rect {
             
      
        [self.placeholder drawInRect:rect withAttributes:@{
             NSForegroundColorAttributeName: [UIColor purpleColor], NSFontAttributeName: [UIFont systemFontOfSize:13]}];
      }
      

    适配iOS 13时,可根据实际情况选取后两种方案解决闪退问题。如果项目中重复使用了同一种UITextField的样式,推荐使用第三种方案,创建UITextField的子类。

    修改建议: 采用第二种方案,创建UITextFieldCategory文件,里面封装好修改placeholder的方法,后续修改都可统一直接调用这些方法

    // UITextField+CIPlaceholder.m文件
    
    #import "UITextField+CIPlaceholder.h"
    
    @implementation UITextField (CIPlaceholder)
    
    - (void)setPlaceholderFont:(UIFont *)font {
         
    
      [self setPlaceholderColor:nil font:font];
    }
    
    - (void)setPlaceholderColor:(UIColor *)color {
         
    
      [self setPlaceholderColor:color font:nil];
    }
    
    - (void)setPlaceholderColor:(nullable UIColor *)color font:(nullable UIFont *)font {
         
    
      if ([self checkPlaceholderEmpty]) {
         
          return;
      }
    
      NSMutableAttributedString *placeholderAttriString = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
      if (color) {
         
          [placeholderAttriString addAttribute:NSForegroundColorAttributeName value:color range:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值