针对iOS13一些适配问题的处理整理,持续更新

以下是iOS13一些适配问题的暂时解决方案,整理以下,希望能帮到大家。

UITextField 异常

在设置leftView 左按钮时,如果使用的是UIImageView,UIImageView的大小会自动根据图片大小自动缩放内容大小,即会出现图片无法按照意图显示的问题


// Confuse in iOS13
UIImageView *imageIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
//search_icon  19*19
imageIcon.image = [UIImage imageNamed:@"search"];
imageIcon.contentMode = UIViewContentModeCenter;
UITextField *txtSearch = [[UITextField alloc] init];
txtSearch.leftView = imageIcon;


解决方案

  • 方案一、让UI重新给一套图吧,按照40 * 20 来给就行了
  • 方案二、不使用UIImageView做为leftView
UIImageView *imageIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
//search  19*19
UIView *leftView = [UIView.alloc initWithFrame:CGRectMake(0, 0, 40, 20)];
UIView *contView = [UIView.alloc initWithFrame:CGRectMake(12, 1, 19, 19)];
contView.backgroundColor = [UIColor colorWithPatternImage:ASImage(@"search")];
UITextField *txtSearch = [[UITextField alloc] init];
txtSearch.leftView = imageIcon;

UITextField 通过KVC方式修改空白提示语颜色 崩溃

iOS13中,无法使用KVC修改占位文字的颜色,使用此方式会闪退

[UITextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor”];

解决方案

  • 使用富文本设置UITextField.attributedPlaceholder

控制器模态跳转异常

iOS新增模态方式UIModalPresentationAutomatic,并且修改过场动画上下文机制,默认调整为了卡片样式。


typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationFormSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationCurrentContext API_AVAILABLE(ios(3.2)),
    UIModalPresentationCustom API_AVAILABLE(ios(7.0)),
    UIModalPresentationOverFullScreen API_AVAILABLE(ios(8.0)),
    UIModalPresentationOverCurrentContext API_AVAILABLE(ios(8.0)),
    UIModalPresentationPopover API_AVAILABLE(ios(8.0)) API_UNAVAILABLE(tvos),
    UIModalPresentationBlurOverFullScreen API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios) API_UNAVAILABLE(watchos),
    UIModalPresentationNone API_AVAILABLE(ios(7.0)) = -1,
    UIModalPresentationAutomatic API_AVAILABLE(ios(13.0)) = -2,
};

解决方案

  • 方案一、如果项目中控制器模态跳转不多,可一个个修改,设定vc.modalPresentationStyle = UIModalPresentationFullScreen即可
  • 方案二、如果项目中模态过多或者使用的cocoaPod继承的第三方中也存在此问题而第三方没有维护更新;可使用运行时交换系统方法处理
#import "UIViewController+Presented.h"

@implementation UIViewController (Presented)

+ (void)load {
    vpswizzleMethod([self class], @selector(presentViewController:animated:completion:), @selector(swizzled_presentViewController:animated:completion:));
}

- (void)swizzled_presentViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion {
    // 屏蔽iOS13 模态半推框 导致vc声明周期不调用造成程序逻辑丢失的问题
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self swizzled_presentViewController:vc animated:animated completion:completion];
}

void vpswizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector) {
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

@end

你可能感兴趣的:(针对iOS13一些适配问题的处理整理,持续更新)