一绿映、模態(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 進行審批。