iOS13的一些适配

2019-10-10  本文已影响0人  a浮生若梦a

iOS13不允许 valueForKeysetValue: forKey 获取和设置私有属性

iOS13以前:

// textField
[_textField setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
// textView
[_textView setValue:创建的label的对象 forKey:@"_placeholderLabel"];

iOS13以后可以修改为:

// 通过 runtime 获取对象属性
// instance:textField或者textView对象
// name:属性名字例如(@"_placeholderLabel")
Ivar ivar = class_getInstanceVariable(instance.class, name.UTF8String);
UILabel *label = object_getIvar(aTextField, ivar);
// 通过设置label修改

iOS13之后模态present跳转后页面没有充满屏幕

解决办法:

设置 modalPresentationStyle 为 UIModalPresentationFullScreen

iOS13之后 deviceToken 获取方式改变

NSString *tokenStr = nil;
if (@available(iOS 13.0, *)) {
// 适配iOS13
        NSMutableString *deviceStr = [NSMutableString string];
        const char *bytes = deviceToken.bytes;
        NSInteger count = deviceToken.length;
        for (int i = 0; i < count; i++) {
            [deviceStr appendFormat:@"%02x", bytes[i]&0x000000FF];
        }
        tokenStr = deviceStr;
    }else {
// iOS13以前
        NSString *deviceStr = [deviceToken description];
        if ((deviceStr.length > 0) && ([deviceStr rangeOfString:@"<"].location != NSNotFound)) {
            NSString *tk0 = [deviceStr stringByReplacingOccurrencesOfString:@"<"withString:@""];
            NSString *tk1 = [tk0 stringByReplacingOccurrencesOfString:@">"withString:@""];
            tokenStr = [tk1 stringByReplacingOccurrencesOfString:@" "withString:@""];
        }
    }
NSLog(@"%@",tokenStr);
    
上一篇 下一篇

猜你喜欢

热点阅读