iOS13適配記錄

1、模態(tài)跳轉(zhuǎn)方式:presentViewController

問題如下圖:

解決:跳轉(zhuǎn)前,添加入下一句代碼即可

vc.modalPresentationStyle = UIModalPresentationFullScreen;

別添加錯位置了,是即將跳轉(zhuǎn)的頁面添加,不是self

例如:

IndicationViewController *vc = [[IndicationViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];

runtime優(yōu)化presentViewController方式跳轉(zhuǎn):

1、把如下代碼拷貝到項目之中即可全局解決,跳轉(zhuǎn)使用之前樣式
2、如果某個controller想使用新樣式,則
vc.wg_setModalPresentationStyle = NO;

.h
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (WGPresent)

// 某個控制器想用系統(tǒng)默認(rèn),則設(shè)置NO
@property (nonatomic, assign) BOOL wg_setModalPresentationStyle;

@end

NS_ASSUME_NONNULL_END
.m
#import "UIViewController+WGPresent.h"
#import <objc/runtime.h>

@implementation UIViewController (WGPresent)

+(void)load {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        SEL oldSel = @selector(presentViewController:animated:completion:);
        SEL newSel = @selector(wg_presentViewController:animated:completion:);
        
        Method old = class_getInstanceMethod([self class], oldSel);
        Method new = class_getInstanceMethod([self class], newSel);
        if (class_addMethod([self class], oldSel, method_getImplementation(new), method_getTypeEncoding(new))) {
            
            class_replaceMethod([self class], newSel, method_getImplementation(old), method_getTypeEncoding(old));
        }else {
            
            method_exchangeImplementations(old, new);
        }
    });
}

- (void)wg_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    
    if (@available(iOS 13.0, *)) {
        
        if (viewControllerToPresent.wg_setModalPresentationStyle) {
            
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    [self wg_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

- (BOOL)wg_setModalPresentationStyle {
    
    NSNumber *obj = objc_getAssociatedObject(self, @selector(wg_setModalPresentationStyle));
    return obj ? [obj boolValue] : [self.class wg_GlobalSetModalPresentationStyle];
}

-(void)setWg_setModalPresentationStyle:(BOOL)wg_setModalPresentationStyle {
    
    objc_setAssociatedObject(self, @selector(wg_setModalPresentationStyle), @(wg_setModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
}

//以后迭代版本,想全部用系統(tǒng)之前樣式(排除UIImagePickerController,UIAlertController)
+ (BOOL)wg_GlobalSetModalPresentationStyle {
    
    if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
        
        return NO;
    }
    return YES;
}

@end

2、全局關(guān)閉暗黑模式

1、在Info.plist 文件中,添加UIUserInterfaceStyle key 名字為 User Interface Style 值為String。
2、將UIUserInterfaceStyle key 的值設(shè)置為 Light

3、單個界面不遵循暗黑模式

UIViewController與UIView 都新增一個屬性 overrideUserInterfaceStyle,將 overrideUserInterfaceStyle 設(shè)置為對應(yīng)的模式,則強制限制該元素與其子元素以設(shè)置的模式進行展示,不跟隨系統(tǒng)模式改變進行改變

4、通過 KVC來修改一些沒有暴露出來的屬性,崩潰

[self.importCertificateNumTextFiled setValue:kGrayColor forKeyPath:@"_placeholderLabel.textColor"];

修改為

if (@available(iOS 13.0, *)) {
    self.importCertificateNumTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"請輸入" attributes:@{NSForegroundColorAttributeName : kGrayColor}];
}else{
    [self.importCertificateNumTextFiled setValue:kGrayColor forKeyPath:@"_placeholderLabel.textColor"];
}

5、適配Dark Mode

6、UITabBar title選中顏色被還原,出現(xiàn)問題

在設(shè)置UITabBar的地方添加如下代碼

 [[UITabBar appearance] setUnselectedItemTintColor: UIColor.grayColor];

例如我的代碼:

- (UINavigationController *)viewController:(UIViewController *)vc title:(NSString *)title nomalImg:(NSString *)imgStr tag:(NSInteger)tag{
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgStr] tag:tag];
    item.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",imgStr,@"s"]];
    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColor.redColor, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    [[UITabBar appearance] setUnselectedItemTintColor:UIColor.grayColor];
    
    vc.tabBarItem = item;
    return nav;
}

參考:
iOS13 暗黑模式(Dark Mode)適配之OC版
iOS 13 問題解決以及蘋果登錄,暗黑模式
iOS13 DarkMode適配(一)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 每天讀一遍,連續(xù)21天,你將獲得宇宙無限的正能量 一秦東魁 從今天開始的每一天 我已經(jīng)改變成為一個全新的人 我充滿...
    秦東魁閱讀 6,375評論 0 2
  • 桌子老舊, 但可以擺得下咖啡和面包; 陽臺朝北,風(fēng)景不美, 但可以安靜的看書和賞雨。 生活不在于你擁有多少, 身處...
    牧佟閱讀 516評論 0 1
  • 親愛的爸爸:我在學(xué)校里的事情、責(zé)任可能要越來越沉重了,也少有回家的機會,缺乏和你溝通的機會。通過寫信來和您說說話,...
    浩漠閱讀 498評論 0 2