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è)置的模式進(jìn)行展示宏赘,不跟隨系統(tǒng)模式改變進(jìn)行改變
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適配(一)