iOS13 適配

一绿映、模態(tài)彈窗樣式適配

/*
 Defines the presentation style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter.
 If this property has been set to UIModalPresentationAutomatic, reading it will always return a concrete presentation style. By default UIViewController resolves UIModalPresentationAutomatic to UIModalPresentationPageSheet, but system-provided subclasses may resolve UIModalPresentationAutomatic to other concrete presentation styles. Participation in the resolution of UIModalPresentationAutomatic is reserved for system-provided view controllers.
 Defaults to UIModalPresentationAutomatic on iOS starting in iOS 13.0, and UIModalPresentationFullScreen on previous versions. Defaults to UIModalPresentationFullScreen on all other platforms.
 */
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle API_AVAILABLE(ios(3.2));

添加下面等類別即可統(tǒng)一修改:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (MXRModalPresentationStyle)
#ifdef __IPHONE_13_0
@property (nonatomic, assign) BOOL mxr_didSetModalPresentationStyle API_AVAILABLE(ios(13.0));
#endif

@end

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

@implementation UIViewController (MXRModalPresentationStyle)

#ifdef __IPHONE_13_0

+ (void)load {
    if (@available(iOS 13.0, *)) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self mxr_swizzlingInstanceMethodWithClass:self.class
                                                 originSel:@selector(setModalPresentationStyle:)
                                              swizzlingSel:@selector(mxr_setModalPresentationStyle:)];
            [self mxr_swizzlingInstanceMethodWithClass:self.class
                                             originSel:@selector(presentViewController:animated:completion:)
                                          swizzlingSel:@selector(mxr_presentViewController:animated:completion:)];
        });
    }
}

static char MXRDidSetModalPresentationStyle;

- (void)setMxr_didSetModalPresentationStyle:(BOOL)mxr_didSetModalPresentationStyle {
    objc_setAssociatedObject(self, &MXRDidSetModalPresentationStyle, @(mxr_didSetModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)mxr_didSetModalPresentationStyle {
    id obj = objc_getAssociatedObject(self, &MXRDidSetModalPresentationStyle);
    if (obj) {
        return [obj boolValue];
    } else {
        return NO; // default value.
    }
}

- (void)mxr_setModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
    if (@available(iOS 13.0, *)) {
        self.mxr_didSetModalPresentationStyle = YES;
    }
    [self mxr_setModalPresentationStyle:modalPresentationStyle];
}

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

#pragma mark - Method Swizzling

+ (void)mxr_swizzlingInstanceMethodWithClass:(Class)cls originSel:(SEL)originSel swizzlingSel:(SEL)swizzlingSel {
    Method originMethod = class_getInstanceMethod(cls, originSel);
    Method swizzlingMethod = class_getInstanceMethod(cls, swizzlingSel);
    BOOL didAddMethod = class_addMethod(cls,
                                        originSel,
                                        method_getImplementation(swizzlingMethod),
                                        method_getTypeEncoding(swizzlingMethod));
    if (didAddMethod) {
        class_replaceMethod(cls,
                            swizzlingSel,
                            method_getImplementation(originMethod),
                            method_getTypeEncoding(originMethod));
    } else {
        method_exchangeImplementations(originMethod, swizzlingMethod);
    }
}
#endif

@end

二喧笔、暗黑模式

全局關(guān)閉暗黑模式
在Info.plist添加:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

或者

//強制關(guān)閉暗黑模式
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
    if(@available(iOS 13.0,*)){
        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
#endif

單個界面不遵循暗黑模式

在iOS13,為UIViewController和UIView擴展了一個新的API

@property(nonatomic) UIUserInterfaceStyle overrideUserInterfaceStyle;

將 overrideUserInterfaceStyle 設(shè)置為對應(yīng)的模式细卧,則強制限制該元素與其子元素以設(shè)置的模式進行展示谈火,不跟隨系統(tǒng)模式改變進行改變
設(shè)置 ViewController 的該屬性, 將會影響視圖控制器的視圖和子視圖控制器采用該樣式
設(shè)置 View 的該屬性肌幽, 將會影響視圖及其所有子視圖采用該樣式
設(shè)置 Window 的該屬性晚碾, 將會影響窗口中的所有內(nèi)容都采用樣式,包括根視圖控制器和在該窗口中顯示內(nèi)容的所有演示控制器(UIPresentationController)

開啟暗黑模式

+ (void)adaptDarkMode:(UIView *)view lightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
#ifdef __IPHONE_13_0
    if (@available(iOS 13.0, *)) {
        view.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return darkColor;
            } else {
                return lightColor;
            }
        }];
    } else {
        view.backgroundColor = lightColor;
    }
#endif
}

模擬器調(diào)試
運行項目
點擊Xcode底部調(diào)試欄中Environment Overrides
開啟Interface Style喂急,就可以切換了格嘁。

三、廢棄MPMoviePlayerViewController

使用AVPlayerViewController 來替換MPMoviePlayerViewController控件

四廊移、Sign In with Apple

Sign In with Apple will be available for beta testing this summer. It will be required as an option for users in apps that support third-party sign-in when it is commercially available later this year.
如果你的應(yīng)用支持使用第三方登錄糕簿,那么就必須加上蘋果新推出的登錄方式:Introducing Sign In with Apple。

五狡孔、私有方法 KVC 不允許使用

在 iOS 13 中不再允許使用 valueForKey懂诗、setValue:forKey: 等方法獲取或設(shè)置私有屬性,雖然編譯可以通過苗膝,但是在運行時會直接崩潰殃恒,并提示一下崩潰信息

// 使用的私有方法
[_textField setValue:[UIColor redColor] forKeyPath:@“_placeholderLabel.textColor"];
// 崩潰提示信息
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug' 
// 替換的方案
_textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"輸入"attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}]; 

六、LaunchImage 被棄用

iOS 8 之前我們是在LaunchImage 來設(shè)置啟動圖荚醒,但是隨著蘋果設(shè)備尺寸越來越多芋类,我們需要在對應(yīng)的 aseets 里面放入所有尺寸的啟動圖,這是非常繁瑣的一個步驟界阁。因此在 iOS 8 蘋果引入了 LaunchScreen.storyboard侯繁,支持界面布局用的 AutoLayout + SizeClass ,可以很方便適配各種屏幕泡躯。
需要注意的是贮竟,蘋果在 Modernizing Your UI for iOS 13 section 中提到,從2020年4月開始较剃,所有支持 iOS 13 的 App 必須提供 LaunchScreen.storyboard咕别,否則將無法提交到 App Store 進行審批。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末写穴,一起剝皮案震驚了整個濱河市惰拱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌啊送,老刑警劉巖偿短,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件欣孤,死亡現(xiàn)場離奇詭異,居然都是意外死亡昔逗,警方通過查閱死者的電腦和手機降传,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勾怒,“玉大人婆排,你說我怎么就攤上這事”柿矗” “怎么了段只?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長卡乾。 經(jīng)常有香客問我翼悴,道長,這世上最難降的妖魔是什么幔妨? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任鹦赎,我火速辦了婚禮,結(jié)果婚禮上误堡,老公的妹妹穿的比我還像新娘古话。我一直安慰自己,他們只是感情好锁施,可當我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布陪踩。 她就那樣靜靜地躺著,像睡著了一般悉抵。 火紅的嫁衣襯著肌膚如雪肩狂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天姥饰,我揣著相機與錄音傻谁,去河邊找鬼。 笑死列粪,一個胖子當著我的面吹牛审磁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播岂座,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼态蒂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了费什?” 一聲冷哼從身側(cè)響起钾恢,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后瘩蚪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刑桑,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年募舟,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闻察。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡拱礁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辕漂,到底是詐尸還是另有隱情呢灶,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布钉嘹,位于F島的核電站鸯乃,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏跋涣。R本人自食惡果不足惜缨睡,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望陈辱。 院中可真熱鬧奖年,春花似錦、人聲如沸沛贪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽利赋。三九已至水评,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間媚送,已是汗流浹背中燥。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留季希,地道東北人褪那。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像式塌,于是被迫代替她去往敵國和親博敬。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,601評論 2 353