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];

 

發佈了209 篇原創文章 · 獲贊 235 · 訪問量 193萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章