iOS13适配宝典

239 阅读5分钟

适配问题一

  • UITextField 的私有属性 _placeholderLabel 被禁止访问了
[self.textField setValue:self.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

居然崩溃了,错误信息如下

'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug' 

解决方案:

方案A:
 
UITextField有个attributedPlaceholder的属性,我们可以自定义这个富文本来达到我们需要的结果。
 
NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : self.placeholderColor}];
_textField.attributedPlaceholder = placeholderString;
 
 
方案B:
 
#import <objc/runtime.h>
 
 
Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
UILabel *placeholderLabel = object_getIvar(textField, ivar);
placeholderLabel.textColor = [UIColor whiteColor];

iOS 13 通过 KVC 方式修改私有属性,有 Crash 风险,谨慎使用!并不是所有KVC都会Crash,要尝试!

适配问题二

控制器的 modalPresentationStyle 默认值变了

查阅了下 UIModalPresentationStyle枚举定义,赫然发现iOS 13新加了一个枚举值:
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationFormSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationCurrentContext API_AVAILABLE(ios(3.2)),
    UIModalPresentationCustom API_AVAILABLE(ios(7.0)),
    UIModalPresentationOverFullScreen API_AVAILABLE(ios(8.0)),
    UIModalPresentationOverCurrentContext API_AVAILABLE(ios(8.0)),
    UIModalPresentationPopover API_AVAILABLE(ios(8.0)) API_UNAVAILABLE(tvos),
    UIModalPresentationBlurOverFullScreen API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios) API_UNAVAILABLE(watchos),
    UIModalPresentationNone API_AVAILABLE(ios(7.0)) = -1,
    UIModalPresentationAutomatic API_AVAILABLE(ios(13.0)) = -2,
};

解决方案

方案A:
如果你完全接受苹果的这个默认效果,那就不需要去修改任何代码
如果,你原来就比较细心,已经设置了modalPresentationStyle的值,那你也不会有这个影响
对于想要找回原来默认交互的同学,直接设置如下即可:
 
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
 
 
方案B:
如果要一次全局更改的话,设置UIViewController Catagory即可以解决:
 
@implementation UIViewController(modalPresentationStyle)
 
- (UIModalPresentationStyle)modalPresentationStyle{
    if (@available(iOS 13.0, *)) {
        if ([self isKindOfClass:[UIActivityViewController class]]) {
            return UIModalPresentationPageSheet;
        }else{
            return UIModalPresentationFullScreen;
        }
    }
    return UIModalPresentationPopover;
}

适配问题三

MPMoviePlayerController 在iOS 13已经不能用了

'MPMoviePlayerController is no longer available. Use AVPlayerViewController in AVKit.' 

解决方案:

既然不能再用了,那只能换掉了。替代方案就是AVKit里面的那套播放器。

适配问题四

iOS 13 DeviceToken有变化

NSString *dt = [deviceToken description];
dt = [dt stringByReplacingOccurrencesOfString: @"<" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @">" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @" " withString: @""];
这段代码运行在 iOS 13 上已经无法获取到准确的DeviceToken字符串了,iOS 13 通过[deviceToken description]获取到的内容已经变了。

解决方案

方案A:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = [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);
}
 
方案B:
 
NSMutableString *deviceTokenString = [NSMutableString string];
const char *bytes = deviceToken.bytes;
NSInteger count = deviceToken.length;
for (int i = 0; i < count; i++) {
    [deviceTokenString appendFormat:@"%02x", bytes[i]&0x000000FF];
}

适配问题五

Sign in with Apple -提供第三方登录的注意啦

如果你的应用使用了第三方登录,那么你可能也需要加下 「Sign in with Apple」
Sign In with Apple will be available for beta testing this summer. It will be required as an option for users in apps that support third-party sign-in when it is commercially available later this year.

解决方案 附上官方Demo:点我下载

适配问题六

即将废弃的 LaunchImage

注意啦⚠️,从2020年4月开始,所有使⽤ iOS13 SDK的 App将必须提供 LaunchScreen,LaunchImage即将退出历史舞台。

适配问题七

UISegmentedControl在iOS 13中具有新外观,并且用于改变分段控件颜色的现有代码不再像以前那样工作。

在iOS 13之前,您可以设置tintColor和将用于分段控件周围的边框,段之间的线以及所选段的背景颜色。然后,您可以使用前景色属性更改每个片段的标题颜色titleTextAttributes。

在iOS 13下,tintColor什么都不做。您可以设置分段控件backgroundColor以更改分段控件的整体颜色。但我找不到任何方法来改变用作所选片段背景的颜色。设置文本属性仍然有效。我甚至尝试设置标题的背景颜色,但这只影响标题的背景,而不影响所选片段背景颜色的其余部分。

简而言之,如何修改UISegmentedControliOS 13中当前所选段的背景颜色?是否有适当的解决方案,使用公共API,不需要深入私有子视图结构?

iOS 13中没有新属性,UISegmentedControl或者UIControl没有任何相关的更改UIView。

我尝试了解决方法,它对我很有用这是Objective C版本:
 
@interface UISegmentedControl (Common)
- (void)ensureiOS12Style;
@end
 
 
@implementation UISegmentedControl (Common)
- (void)ensureiOS12Style {
    // UISegmentedControl has changed in iOS 13 and setting the tint
    // color now has no effect.
    if (@available(iOS 13, *)) {
        UIColor *tintColor = [self tintColor];
        UIImage *tintColorImage = [self imageWithColor:tintColor];
        // Must set the background image for normal to something (even clear) else the rest won't work
        [self setBackgroundImage:[self imageWithColor:self.backgroundColor ? self.backgroundColor : [UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:tintColorImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:[self imageWithColor:[tintColor colorWithAlphaComponent:0.2]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:tintColorImage forState:UIControlStateSelected|UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [self setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor, NSFontAttributeName: [UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
        [self setDividerImage:tintColorImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        self.layer.borderWidth = 1;
        self.layer.borderColor = [tintColor CGColor];
    }
}
 
- (UIImage *)imageWithColor: (UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
@end
 
 
swift版本:
 
extension UISegmentedControl {
    /// Tint color doesn't have any effect on iOS 13.
    func ensureiOS12Style() {
        if #available(iOS 13, *) {
            let tintColorImage = UIImage(color: tintColor)
            // Must set the background image for normal to something (even clear) else the rest won't work
            setBackgroundImage(UIImage(color: backgroundColor ?? .clear), for: .normal, barMetrics: .default)
            setBackgroundImage(tintColorImage, for: .selected, barMetrics: .default)
            setBackgroundImage(UIImage(color: tintColor.withAlphaComponent(0.2)), for: .highlighted, barMetrics: .default)
            setBackgroundImage(tintColorImage, for: [.highlighted, .selected], barMetrics: .default)
            setTitleTextAttributes([.foregroundColor: tintColor, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
            setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
            layer.borderWidth = 1
            layer.borderColor = tintColor.cgColor
        }
    }
}

适配问题八

UITableViewCell的UITableViewCellAccessoryDisclosureIndicator显示不正常,出现方框显示

原因:项目中有UIImage的catagory,且含有方法:imageWithTintColor,这个方法会和ios13新api冲突,导致出现方框

解决方案A:
 
cell.accessoryView = [[UIImageView alloc] initWithImage:...];
 
使用图片来显示。
 
解决方案B:
 
修改分类中的imageWithTintColor函数名,使之不与系统的冲突。

原文作者:new_dev

原文地址:blog.csdn.net/u012094456/…