iOS13适配总结

1、UITextField修改placeholder颜色
私有属性_placeholderLabel 被禁止访问了

Swift:
textField.setValue(.white, forKeyPath: "_placeholderLabel.textColor")
修改为
let attributedString = NSMutableAttributedString(string: "输入", attributes: [NSAttributedString.Key.foregroundColor : BaseColor.PlaceHolderColor])
textField.attributedPlaceholder = attributedString

OC:
[textField setValue:[UIColor white] forKeyPath:@"_placeholderLabel.textColor"];
修改为
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入" attributes:@{NSForegroundColorAttributeName: [UIColor red]}];
2、present控制器模态跳转样式
Swift:
self.modalPresentationStyle = .fullScreen
 
OC:
self.modalPresentationStyle = UIModalPresentationFullScreen;
3、全局关闭暗黑模式
在info.plist中添加User Interface Style,设置为Light
或者:
if(@available(iOS 13.0,*)) {
self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
4、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);
5、废弃的 LaunchImage
从2020年4月开始,所有使⽤ iOS13 SDK的App将必须使用LaunchScreen
6、UISegmentedControl变化
iOS13默认样式是白底黑字,不能通过tintColor修改选中背景颜色,新增了selectedSegmentTintColor属性用以修改选中的颜色
7、CNCopyCurrentNetworkInfo
iOS13 以后只有开启了 Access WiFi Information capability,才能获取到SSID和BSSID,应用还需要符合下列三项条件中的至少一项才能得到正确的值:
1、获得定位服务权限。
2、使用NEHotspotConfiguration来配置 WiFi 网络的应用。
3、目前正处于启用状态的 VPN 应用。

Swift:
func getWifiName() -> String? {
    
    var wifiName = ""
    let wifiInterfaces = CNCopySupportedInterfaces()
    if wifiInterfaces == nil {
        return nil
    }
    
    let interfaceArr = CFBridgingRetain(wifiInterfaces!) as! Array
    
    if interfaceArr.count > 0 {
        
        let interfaceName = interfaceArr[0] as CFString
        let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
        
        if (ussafeInterfaceData != nil) {
            
            let interfaceData = ussafeInterfaceData as! Dictionary
            wifiName = interfaceData["SSID"]! as! String
        }
    }
    return wifiName
}

OC:
-(void)getWiFiName {
    
    NSString *wifiName = nil;
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
      if (!wifiInterfaces) {
        return nil;
    }
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
    for (NSString *interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
     
        if (dictRef) {
            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
            NSLog(@"network info -> %@", networkInfo);
            wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
            CFRelease(dictRef);
            }
        }
        CFRelease(wifiInterfaces);
        return wifiName;
    }
}
8、废弃UIWebView,WKWebView 中测量页面内容高度的方式变更
=iOS13
document.documentElement.scrollHeight
9、UISearchBar 黑线处理导致崩溃
之前为了处理搜索框的黑线问题,通常会遍历 searchBar 的 subViews,找到并删除 UISearchBarBackground,在 iOS13 中这么做会导致 UI 渲染失败,然后直接崩溃
解决办法:设置 UISearchBarBackground 的 layer.contents 为 nil
for (UIView *view in _searchBar.subviews.lastObject.subviews) {
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        view.layer.contents = nil;
        break;
    }
} 

//崩溃 api
UITextField *textField = [searchBar valueForKey:@"_searchField"];
//方案1:使用 iOS 13 的新属性 searchTextField
searchBar.searchTextField.placeholder = @"search";
//方案2:遍历获取指定类型的属性
- (UIView *)findViewWithClassName:(NSString *)className inView:(UIView *)view{
    Class specificView = NSClassFromString(className);
    if ([view isKindOfClass:specificView]) {
        return view;
    }

    if (view.subviews.count > 0) {
        for (UIView *subView in view.subviews) {
            UIView *targetView = [self findSpecificView:className inView:subView];
            if (targetView != nil) {
                return targetView;
            }
        }
    }
    
    return nil;
}
// 调用方法
UITextField *textField = [self findViewWithClassName:@"UITextField" inView:_searchBar];

//崩溃 api
[searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
// 替代方案,用同上的方法找到子类中 UIButton 类型的属性,然后设置其标题
UIButton *cancelButton = [self findViewWithClassName:NSStringFromClass([UIButton class]) inView:searchBar];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
10、iOS13中使用UISearchDisplayController崩溃
使用 UISearchController 替换 UISearchBar + UISearchDisplayController组合
11、UITabbar无法通过设置shadowImage去掉上面的线
Swift:
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()
}

OC:
if (@available(iOS 13.0, *)) {
    UITabBarAppearance* appearance =  self.tabBar.standardAppearance.copy;
    appearance.backgroundImage = [UIImage new];
    appearance.shadowImage = [UIImage new];
    appearance.shadowColor = [UIColor clearColor];
    // Title adjustment
    appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -12);
    self.tabBar.standardAppearance = appearance;
} else {
    self.tabBar.shadowImage = [UIImage new];
    self.tabBar.backgroundImage = [UIImage new];
}
12、iOS13菜单栏文字颜色变蓝问题
if #available(iOS 13.0, *) {
    UITabBar.appearance().unselectedItemTintColor = UIColor(hex: 0x888888)
    UITabBar.appearance().tintColor = UIColor(hex: 0x333333)
}

你可能感兴趣的:(iOS13适配总结)