iOS 13适配

1,618 阅读2分钟

iOS 13正式版什么时候发布?

09-20

• 第一批 iPhone 开始发货 • 第一批 Apple Watch 5 正式发货 • iOS 13 正式版发布

问题

发现在iOS 13 beta深色外观,我们的APP有些页面显示有问题。例如,视图背景和Label文字颜色都是黑色的,或者都是白色的,显示不出来,从而影响业务功能。

计划用2个版本适配

第一个版本,5秒快速适配,发版。

第二个版本,增加APP深色外观的配色,发版。

5秒快速适配(不应用深色)

在Info.plist添加一个键-值对:UIUserInterfaceStyle-Light。需要用Xcode 11 GM或者Xcode 11版本打包和上传到App Store。如果没有这个版本,就不能用添加键值对这种方式,可以用下面这种方式。

#ifdef __IPHONE_13_0
    if (@available(iOS 13, *)) {
        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
#endif

通过上面2种方式,在深色外观时,系统会忽略对我们APP应用深色外观。但是,苹果不推荐这样,鼓励我们适配深色,这可以作为一个过渡方案。

APP不应用深色

APP不适配深色外观,基于2种可能情况的考虑。一是APP适配深色改造成本很大,二是为了保持APP的一致外观。怎么做呢?给每个UI的控件都设置背景颜色和字体颜色,或者其他属性。下面列举一些,其他请自己注意类推。

//UILabel设置字体颜色;
label.textColor = kZhengWenColor;

//UITextField设置字体颜色和attributedPlaceholder
textField.textColor = kZhengWenColor;
textField.attributedPlaceholder = [[NSMutableAttributedString alloc]
                                      initWithString:@" "
                                      attributes:@{NSForegroundColorAttributeName: kFuZhuWenZiColor}];
textField.placeholder = @"textField";

//UITableView设置背景颜色和cell的背景颜色
myTableView.backgroundColor = [UIColor colorWithHexString:@"F2F2F2"];
cell.backgroundColor = UIColor.whiteColor;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textColor = kZhengWenColor;

//UIDatePicker设置背景颜色
myDatePicker.backgroundColor = [UIColor grayColor];

另外,如果项目中有使用groupTableViewBackgroundColor,需要替换成colorWithHexString:@"F2F2F2"

APP应用深色

苹果鼓励我们应用深色。怎么做呢?颜色适配应用colorWithDynamicProvider方法,图片适配需要提供新的深色图片。

//UITableView自动切换背景颜色
self.tabV.backgroundColor = UIColor.whiteColor;
#ifdef __IPHONE_13_0
    if (@available(iOS 13, *)) {
        self.tabV.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return UIColor.blackColor;
            } else {
                return UIColor.whiteColor;
            }
        }];
    }
#endif
//UIDatePicker自动切换背景颜色
self.myDatePicker.backgroundColor = UIColor.whiteColor;
#ifdef __IPHONE_13_0
    if (@available(iOS 13, *)) {
        self.myDatePicker.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return UIColor.blackColor;
            } else {
                return UIColor.whiteColor;
            }
        }];
    }
#endif

iOS 13的其他改变

//禁止访问私有属性和方法
[self.textField setValue:self.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
//替换为
NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : self.placeholderColor}];
_textField.attributedPlaceholder = placeholderString;
检索条件:@"_或者forKeyPath:@"_

//导航栏的右上角的按钮文字颜色,需要用属性tintColor
[UINavigationBar appearance].tintColor = UIColor.whiteColor;

苹果禁用UIWebView,需要替换为WKWebView。否则审核被拒。

如果APP使用手机蓝牙,必须在info.plist新加描述:
NSBluetoothAlwaysUsageDescription:你们自己的描述

参考

developer.apple.com/documentati…