iOS 这些年新增的API

老开发补课

1. iOS 11 UIView 圆角

typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
  kCALayerMinXMinYCorner = 1U << 0, // 左上
  kCALayerMaxXMinYCorner = 1U << 1, 右上
  kCALayerMinXMaxYCorner = 1U << 2, // 左下
  kCALayerMaxXMaxYCorner = 1U << 3, // 右下
};
CACornerMask 如同上面的UIRectCorner 可以使用位运算进行圆角设置

if (@available(iOS 11.0, *)) {
        view.layer.cornerRadius = radius;
        view.layer.maskedCorners = kCALayerMinXMinYCorner; // 左上圆角
}

2. iOS 11 UITableview 左滑/右划 自定义

右滑

#ifdef __IPHONE_11_0
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (@available(iOS 11.0, *)) {
        UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
              //响应事件在这里操作
        }];
        //设置图片,但是设置不了原图,都是被默认为白色了,字体也是
        UIImage *image =  [[UIImage imageNamed:@"ico_delete"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [deleteRowAction setImage:image];
        deleteRowAction.backgroundColor = [UIColor redColor];

        UIContextualAction *editRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
              //响应事件在这里操作
        }];
        editRowAction.image = [UIImage imageNamed:@"ico_edit"];
        editRowAction.backgroundColor = [UIColor blueColor];
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,editRowAction]];
        //设置全屏滑动时不自定响应事件
        config.performsFirstActionWithFullSwipe = false;
        return config;
    }else{
        return nil;
    }
}

左滑

- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (@available(iOS 11.0, *)) {
        UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        }];
        UIImage *image =  [[UIImage imageNamed:@"ico_delete"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [deleteRowAction setImage:image];
        deleteRowAction.backgroundColor = [UIColor redColor];
        UIContextualAction *editRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        }];
        editRowAction.image = [UIImage imageNamed:@"ico_edit"];
        editRowAction.backgroundColor = [UIColor blueColor];

        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,editRowAction]];
        config.performsFirstActionWithFullSwipe = false;
        return config;
    }else{
        return nil;
    }
}

3. iOS 11 UICollectionView/UITabelView reloadData,但不会试视图失去第一响应者(失去焦点、键盘收起)

// Allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.
- (void)performBatchUpdates:(void (NS_NOESCAPE ^ _Nullable)(void))updates completion:(void (^ _Nullable)(BOOL finished))completion NS_SWIFT_DISABLE_ASYNC API_AVAILABLE(ios(11.0), tvos(11.0));

使用这个方法让UICollectionView/UITabelView进行重新布局,相当于调用[collectionView reloadData方法,但是UITextField等控件不会失去焦点仍然是第一响应者,从而解决了重新布局导致键盘收起的问题。

iOS 11 UIStackView 可以定制各个子控件后面的间距

- (void)setCustomSpacing:(CGFloat)spacing afterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));
- (CGFloat)customSpacingAfterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));

5. iOS 15 sectionHeaderTopPadding

if (@available (ios 15.0, *) ) {
    tableView.sectionHeaderTopPadding = 0;
}