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
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
@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适配(一)