iOS13測試版發(fā)布以后,老大讓我趕緊也適配一下,就一番折騰,安裝了ios13Btea測試版,Xcode直接添加ios13真機包,一運行,沒有任何問題,網(wǎng)上說的TextFIeld 的KVC會崩潰,模態(tài)跳轉的問題完全都不存在
重點:也就是說iOS13是完全兼容Xcode11之前的版本的,只有通過 Xcode11編譯運行的才會有上述問題
以下問題都是建立在Xcode11,iOS13之上的
TextFIeld KVC的問題
//iOS13以前我們可以通過KVC 修改TextFiled的內部屬性,例如:
[self.txtPhone setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.txtPassword setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//但是,在iOS13之后就會崩潰,需要改成下面這樣:
self.txtPhone.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"登錄手機號"attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
self.txtPassword.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"請輸入密碼"attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
presentViewController 的問題
iOS13之前我們只要設置如下就可以了,至于模態(tài)的方式一般默認,有需要再調整:
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor orangeColor];
[self presentViewController:vc animated:YES completion:nil];
但是,iOS13以后上面這個代碼,只模態(tài)出小卡片形式,如果需要之前一樣還是滿屏的的模態(tài)可設置如下:
vc.modalPresentationStyle = UIModalPresentationFullScreen;
這里還有個問題就是:在dismiss vc的時候,不會再走vc 的這兩個方法,如果在這兩個方法中有初始化或者重要邏輯的,就需要修改了
- (void)viewWillAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
崩潰問題
[_LSDefaults sharedInstance]: unrecognized selector sent to class 0x1dfc01258
代碼運行起來0~2分鐘內,必然會出現(xiàn)這個崩潰,代碼里所有sharedInstance的地方加了斷點一個都沒走,很絕望,最后通過注釋代碼的方式,終于找到問題所在了,是由于接入UM, 在設置UM的appkey的時候崩潰的:
[UMConfigure initWithAppkey:@"" channel:nil];
解決方法:升級UMCCommon到最新的2.1.1版本,最好呢,繼承UM的庫都升級到最新版本
DeviceToken 的問題
菜鳥第一次從官網(wǎng)下載了一個iOS13 public測試版本的描述文件,安裝以后,真機運行完全都不會走這個方法:
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
證書,push開關這些都沒問題的,因為在非iOS13的手機上都是沒問題的,也在開發(fā)群里咨詢了大神,他們的都是可以的沒有問題,最后菜鳥只能退回到iOS12.4, 真機運行時沒問題的,說明手機沒問題,再次下載iOS13 Beta5描述文件安裝以后,再真機就能獲取到deviceToken了,我也很無奈、、、、
iOS13 DeviceToken獲取方法,來源于UM:
#include <arpa/inet.h>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
if (![deviceToken isKindOfClass:[NSData class]]) return;
const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
NSLog(@"deviceToken:%@",hexToken);
}
iOS 獲取
Dark 模式適配問題
在Dark模式下, app沒有設置背景顏色的控件會呈現(xiàn)Dark模式的顏色,字體顏色會根據(jù)Dark模式自動變化。
設置了背景色的地方就需要進行適配:
UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trainCollection) {
if ([trainCollection userInterfaceStyle] == UIUserInterfaceStyleLight) {
return [UIColor redColor];
}
else {
return [UIColor greenColor];
}
}];
[self.bgView setBackgroundColor:dyColor];
但是項目中,設置背景的地方太多了,改起來工作量很大,我試圖通過Method Swizzling 進行重寫:
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
來達到適配dark,但是很遺憾,這樣做雖然能夠解決部分背景,但是還有一部分還是需要單獨設置,尤其是字體顏色
圖片的適配:
那有人說,適配這個Dark模式太浪費時間了,要等UI重新設計, 效果還不一定好,那能不能強制設置Light(非Dark)模式呢
很肯定的告訴你:可以的
只需要在info.plist里加入:UserInterfaceStyle : UIUserInterfaceStyleLight
那單個VC可不可設置強制設置呢
也是可以的:
self.overrideUserInterfaceStyle = UIUserInterfaceStyleDark
TabBar 字體顏色的問題:
- Dark模式下,跳轉界面返回以后,tabBarItem的字體變成了藍色,改不了
跳轉前:
跳轉返回后:
可以過一下代碼解決:
//未選中下的顏色(ios10以后才有)
UITabBar.appearance.unselectedItemTintColor = [UIColor redColor];
//選中下的顏色(代替selectedImageTintColor)
UITabBar.appearance.tintColor = [UIColor orangeColor];
- tabBar的setShadowImage不起作用,去不掉上邊線
UITabBarController * tabBC = [[UITabBarController alloc] init];
[tabBC.tabBar setShadowImage:[self createImageWithColor:[UIColor orangeColor]]];
[tabBC.tabBar setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]]];
上述代碼設置了tabBar的shadowImage為 orangeColor顏色的
事件結果如圖,沒有任何的效果,如果不設置默認是黑色的上邊線:
解決辦法:
這里是swift, OC的請依葫蘆畫瓢
if #available(iOS 13, *) {
let appearance = self.tabBar.standardAppearance.copy()
appearance.backgroundImage = UIImage()
appearance.shadowImage = UIImage()
appearance.shadowColor = .clear
self.tabBar.standardAppearance = appearance
} else {
self.tabBar.shadowImage = UIImage()
self.tabBar.backgroundImage = UIImage()
}
title調整使用下面
appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -12
最后感謝 RiversMa 提供的解決Tabbar邊線問題的方法??!
