iOS11适配和填坑 (二)

2017-09-27  本文已影响286人  进阶的蚊子

之前有人说在iOS11下,Xcode9 beta版本下,滚动条高度跳动、上下拉刷新问题,楼主实验了下,并没有遇到,不知道是不是因为楼主的是Xcode9正式版的原因.
为此楼主专门写了些代码实验.先上代码,再详细讲解.

  #import "ViewController.h"

#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define kNavBarHeight 44.0
#define kTabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
#define kTopHeight (kStatusBarHeight + kNavBarHeight)

#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define kScreenWidth [UIScreen mainScreen].bounds.size.width



@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView* tableView;
@property(nonatomic,strong)NSMutableArray* datalist;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _datalist = [NSMutableArray array];
    for(int i=0;i<50;i++){
        [_datalist addObject:@(i)];
    }
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kScreenHeight-kTopHeight-kTabBarHeight)];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    if (@available(iOS 11.0, *)){
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.tableView.showsVerticalScrollIndicator=YES;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    [self.view addSubview:_tableView];
    self.navigationItem.title=@"表格测试滑动";
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



#pragma mark  tableDelegate and dataSourse
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _datalist.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* celid=@"122333";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:celid];
    if(!cell){
        cell=[[UITableViewCell alloc]init];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"haha%ld--%@",indexPath.row,_datalist[indexPath.row]];
    cell.backgroundColor=[UIColor lightGrayColor];
    return cell;
}

#pragma mark 执行删除置顶等操作
//-(UISwipeActionsConfiguration*)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}


-(UISwipeActionsConfiguration*)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除
    //return [self deletAction:indexPath];
    //置顶
    return [self topMoveAction:indexPath];
    
}

//删除
-(UISwipeActionsConfiguration*)deletAction:(NSIndexPath*)indexPath
{
    //删除
    UIContextualAction* delteAction=[UIContextualAction contextualActionWithStyle:(UIContextualActionStyleDestructive) title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self.datalist removeObjectAtIndex:indexPath.row];
        [self.tableView reloadData];
        completionHandler(YES);
        
    }];
    
    //可以设置图片
    //背景色
    delteAction.backgroundColor=[UIColor redColor];
    
    UISwipeActionsConfiguration* config=[UISwipeActionsConfiguration configurationWithActions:@[delteAction]];
    
    return config;
}

//置顶

-(UISwipeActionsConfiguration*)topMoveAction:(NSIndexPath*)indexPath
{
    //置顶
    UIContextualAction* topAction=[UIContextualAction contextualActionWithStyle:(UIContextualActionStyleNormal) title:@"置顶" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self.datalist exchangeObjectAtIndex:0 withObjectAtIndex:indexPath.row];
        [self.tableView reloadData];
        
        completionHandler(YES);
        
    }];
    
    //可以设置图片
    //背景色
    topAction.backgroundColor=[UIColor greenColor];
    
    UISwipeActionsConfiguration* config=[UISwipeActionsConfiguration configurationWithActions:@[topAction]];
    
    return config;
}
@end

因为在iOS11 下 iPhone X 这个有刘海 和下边没有home 键,我暂时不建议使用xib来适配.使用代码布局会更加方便
暂时只考虑没有大标题的,如果有大标题的 加上52高度
于是先定义了几个宏

 //状态栏高度
#define kStatusBarHeight [[UIApplication sharedApplication]    statusBarFrame].size.height
//非iphone 导航栏
#define kNavBarHeight 44.0

//tabbar高度
#define kTabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
//顶部高度
#define kTopHeight (kStatusBarHeight + kNavBarHeight)

//屏幕高
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

//屏幕宽
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

创建tableView 实现协议代理没什么好说的,下面重点说下删除置顶操作.
在iOS8之后,苹果官方增加了UITableVIew的右滑操作接口,即新增了一个代理方法(tableView: editActionsForRowAtIndexPath:)和一个类(UITableViewRowAction),代理方法返回的是一个数组,我们可以在这个代理方法中定义所需要的操作按钮(删除、置顶等),这些按钮的类就是UITableViewRowAction。这个类只能定义按钮的显示文字、背景色、和按钮事件。并且返回数组的第一个元素在UITableViewCell的最右侧显示,最后一个元素在最左侧显示。
从iOS 11开始有了一些改变,首先是可以给这些按钮添加图片了,然后是如果实现了以下两个iOS 11新增的代理方法,将会取代(tableView: editActionsForRowAtIndexPath:)

这2个代理方法是:

  1. 向左滑动在右边显示

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

  1. 向右边滑动在左边显示
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath

一般情况下我们习惯于操作第1种方式.
创建UIContextualAction对象时,UIContextualActionStyle有两种类型,如果是置顶、已读等按钮就使用UIContextualActionStyleNormal类型,delete操作按钮可使用UIContextualActionStyleDestructive类型,当使用该类型时,如果是右滑操作,一直向右滑动某个cell,会直接执行删除操作,不用再点击删除按钮,这也是一个好玩的。
查看UIContextualAction 我们无法修改UIContextualAction对象的字体颜色和大小,不得不说这不是一个好的体验,希望以后苹果爸爸能让自定义样式.

@interface UIContextualAction : NSObject

+ (instancetype)contextualActionWithStyle:(UIContextualActionStyle)style title:(nullable NSString *)title handler:(UIContextualActionHandler)handler;

@property (nonatomic, readonly) UIContextualActionStyle style;
@property (nonatomic, copy, readonly) UIContextualActionHandler handler;

@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) UIColor *backgroundColor; // a default background color is set from the action style
@property (nonatomic, copy, nullable) UIImage *image;

@end

滑动操作这里还有一个需要注意的是,当cell高度较小时,会只显示image,不显示title,当cell高度够大时,会同时显示image和title。

最后附上操作的效果图

置顶效果

Untitled7.gif

删除效果

Untitled8.gif
上一篇下一篇

猜你喜欢

热点阅读