iOS 开发每天分享优质文章iOS UI开发性能优化iOS

开发者须知 iOS13 新特性

2019-09-26  本文已影响0人  Trigger_o

一.Dark Mode

首先说万金油适配方法,如果希望在iOS依旧保持原来的样子,就在plist文件增加key-value,如图

plist文件

1.全局适配
不设置背景颜色的视图,背景颜色会根据系统设置的模式改变
iOS13中uiviewcontroller和uiview新增了overrideUserInterfaceStyle属性,通过设置这个属性可以改变vc或view本身及其子视图的模式(light和dark和unspecified)
也就是说App既可以接受手机系统设置,也可以不接受,由开发者主动设置
如果给window设置,会影响window上的所有vc和view,以及模态处理的视图比如uialertcontroller,不过不建议这么做,最好按照上面说的那样设置plist文件.

模拟器可以动态的修改模式,不过是在xcode上设置,debug状态下才生效,如图


模拟器设置dark模式

2.监听状态

//在vc中重写方法
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    [super traitCollectionDidChange:previousTraitCollection];
    if (@available(iOS 13.0, *)) {
        self.view.backgroundColor = self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? [UIColor blackColor] : [UIColor whiteColor];
        NSLog(@"previousTraitCollection=%ld,overrideUserInterfaceStyle=%ld,self.traitCollection.userInterfaceStyle=%ld",previousTraitCollection.userInterfaceStyle,self.overrideUserInterfaceStyle,self.traitCollection.userInterfaceStyle);
    }
}

切换dark mode后,这里的输出结果是 previousTraitCollection=1,overrideUserInterfaceStyle=0,self.traitCollection.userInterfaceStyle=2,
1.方法中的参数previousTraitCollection是模式改变之前的状态
2.vc的属性overrideUserInterfaceStyle是限定这个vc或view及其子视图的模式,赋值成除UIUserInterfaceStyleUnspecified之外的时候,即使系统修改模式,这个vc重写的traitCollectionDidChange方法也不会被调用
3.获取当前的模式需要使用vc的traitCollection属性中的userInterfaceStyle

3.UIColor
新增了动态颜色
3.1.在assets.xcassets中创建

动态颜色1
动态颜色2

并且提供了五个大种类,很多种预设颜色,System colors, Foreground colors ,Background colors ,Fill colors ,Other colors


预设颜色

3.2.可以用code创建动态颜色
新增了三个方法


UIColor新增方法
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(200, 100, 100, 100)];
    if (@available(iOS 13.0, *)) {
        v.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            return traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? [UIColor whiteColor] : [UIColor blackColor];
        }];
    } else {
        v.backgroundColor = [UIColor yellowColor];
    }
    [self.view addSubview:v];

if (@available(iOS 11.0, *)) {
        UIColor *c = [UIColor colorNamed:@"999999_c"];
    }

999999_c就是assets.xcassets中的命名,注意colorNamed:只在iOS11及以上可以使用此api,colorWithDynamicProvider:只在iOS 13及以上可以使用

4.图片
可以在assets.xcassets设置不同模式下的图片

多种模式下的图片

二.statusBar

现在是三种模式,default是根据系统设置改变


statusBar

适配上来说,之前设置了default的 ,在iOS13上也还是default,不过不再是固定的黑色,之前设置default的如果希望和原来一样,需要改成UIStatusBarStyleDarkContent

三.模态弹出presentViewcontroller

先说适配,可以在公共父类的init中设置modalPresentationStyle 为 UIModalPresentationFullScreen

1.iOS13 presentViewcontroller样式发生变化
UIModalPresentationStyle增加了一个UIModalPresentationAutomatic,并且在iOS13中是默认这个值,而之前默认的是UIModalPresentationFullScreen
UIModalPresentationAutomatic自带手势下拉dismiss

需要注意的是vc高度也发生变化了,并且viewdidload中和实际的高度不一样

新的样式
高度1
高度2

2.生命周期
UIModalPresentationAutomatic和UIModalPresentationFullScreen不同,当A弹出B时,如果是UIModalPresentationFullScreen,A是会调用disappear相关生命周期方法的,B dismiss时,A也会调用appear相关方法,而UIModalPresentationAutomatic不会.

四.KVC不允许使用私有方法

比如UITextField

[textF setValue:DescrbeColor forKeyPath:@"_placeholderLabel.textColor"];

会crash
可以替换成

textF.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:@"  " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:DescrbeColor}];

只需要设置以此,用空格占位,之后设置textF.placeholder就行

五.新建工程

iOS13工程结构发生了变化,有两个代理


新的结构

window不再由APPDelegate管理,这是为iapd os做的准备,普通项目直接在APPDelegate.h中添加一个window就可以了

六.其他新特性

  1. iOS 13 中 tableView 和 collectionView 增加双指滑动编辑的功能

2.新增了文本的手势,在原生控件中默认是生效的,可以禁止,选中文字后,可以操作复制和剪切 ,可以在光标的位置操作粘贴,还可以撤销,反撤销,呼出菜单
重写editingInteractionConfiguration方法可以选择禁止这些手势
复制:三指捏合
剪切:两次三指捏合
粘贴:三指松开
撤销:三指向左划动(或三指双击)
重做:三指向右划动
快捷菜单:三指单击

3.iOS 13新增了UIMenu控件


menu
上一篇下一篇

猜你喜欢

热点阅读