IOS13适配之杂项-持续更新

IOS13升级后接到好多用户反馈,也有自己发现的,整理一下,希望能帮助大家。

UITextField 异常

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

 UIImageView* view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; 
 UIImageView* headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
 headImageView.image = [[UIImage imageNamed:@"task_response_by_spec"]          imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
 headImageView.tintColor = [UIColor whiteColor];
 [view addSubview:headImageView];
    
 _uesrNameTextField.leftView = view;
 _uesrNameTextField.leftViewMode = UITextFieldViewModeAlways;
 _uesrNameTextField.textColor = [UIColor whiteColor];
 [self.view addSubview:_uesrNameTextField];

解决方案

使用View来代替UIImageView作为LeftView

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,_uesrNameTextField.height,_uesrNameTextField.height)];
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(12.5, 12.5, 20, 20)];
    headImageView.image = [[UIImage imageNamed:@"task_response_by_spec"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    headImageView.tintColor = [UIColor whiteColor];
    [view addSubview:headImageView];
    _uesrNameTextField.backgroundColor = [[UIColor colorWithHexString:@"424d5a"] colorWithAlphaComponent:0.7];
    
    _uesrNameTextField.leftView = view;
    _uesrNameTextField.leftViewMode = UITextFieldViewModeAlways;
    _uesrNameTextField.textColor = [UIColor whiteColor];
    [self.view addSubview:_uesrNameTextField];

控制器模态跳转异常

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

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

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

 

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

解决方案

  • 使用富文本设置UITextField.attributedPlaceholder


UICollectionView 注册资源registerNib位置滞后iOS13中出现崩溃

此崩溃是在一个第三方库TZImagePickerController出现的。

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(margin, top, self.view.tz_width - 2 * margin, self.view.tz_height - 50 - top) collectionViewLayout:layout];    
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.alwaysBounceHorizontal = NO;
    if (iOS7Later) _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 2);
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
    _collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 3) / 4) * self.view.tz_width);
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"TZAssetCell" bundle:nil] forCellWithReuseIdentifier:@"TZAssetCell"];

解决方案

把注册的代码提前到UICollectionView初始化后

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(margin, top, self.view.tz_width - 2 * margin, self.view.tz_height - 50 - top) collectionViewLayout:layout];
    //注册nib位置提前,否则在iOS13上报错
    [_collectionView registerNib:[UINib nibWithNibName:@"TZAssetCell" bundle:nil] forCellWithReuseIdentifier:@"TZAssetCell"];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.alwaysBounceHorizontal = NO;
    if (iOS7Later) _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 2);
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
    _collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 3) / 4) * self.view.tz_width);
    [self.view addSubview:_collectionView];

 

你可能感兴趣的:(#,IPhone开发高级系列)