1、模態跳轉方式:presentViewController
問題如下圖:
解決:跳轉前,添加入下一句代碼即可
vc.modalPresentationStyle = UIModalPresentationFullScreen;
別添加錯位置了,是即將跳轉的頁面添加,不是self
例如:
IndicationViewController *vc = [[IndicationViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
runtime優化presentViewController方式跳轉:
1、把如下代碼拷貝到項目之中即可全局解決,跳轉使用之前樣式
2、如果某個controller想使用新樣式,則
vc.wg_setModalPresentationStyle = NO;
.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (WGPresent)
// 某個控制器想用系統默認,則設置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);
}
//以后迭代版本,想全部用系統之前樣式(排除UIImagePickerController,UIAlertController)
+ (BOOL)wg_GlobalSetModalPresentationStyle {
if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
return NO;
}
return YES;
}
@end
2、全局關閉暗黑模式
1、在Info.plist 文件中,添加UIUserInterfaceStyle key 名字為 User Interface Style 值為String。
2、將UIUserInterfaceStyle key 的值設置為 Light
3、單個界面不遵循暗黑模式
UIViewController與UIView 都新增一個屬性 overrideUserInterfaceStyle,將 overrideUserInterfaceStyle 設置為對應的模式,則強制限制該元素與其子元素以設置的模式進行展示,不跟隨系統模式改變進行改變
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選中顏色被還原,出現問題
在設置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適配(一)