置顶按钮(tableview)

列表数据滑动之后,显示置顶按钮(tableView存在分页)
点击置顶按钮之后,列表数据回到最上一行

#define WD_TabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49) //底部tabbar高度

@property(nonatomic,strong) UITableView *debitTable;
@property (nonatomic, strong) UIButton * backTopBtn;//置顶按钮

#pragma mark 创建置顶按钮
-(void)createBackTopBtn{
    self.backTopBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
    [self.backTopBtn setBackgroundImage:[UIImage imageNamed:@"zhiding"] forState:UIControlStateNormal];
    [self.backTopBtn addTarget:self action:@selector(backTopOffset) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backTopBtn];
    [self.backTopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
          make.bottom.mas_equalTo(self.view).offset(-25-WD_TabBarHeight);
          make.right.mas_equalTo(self.view).offset(-15);
          make.width.mas_equalTo(48.5);
          make.height.mas_equalTo(48.5);
     }];
    self.backTopBtn.hidden = YES;
}
- (void)backTopOffset {
    NSIndexPath* indexPat = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.debitTable scrollToRowAtIndexPath:indexPat atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark - 置顶按钮的显示隐藏
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.debitTable) {
        if (self.debitTable.contentOffset.y > 0) {
            self.backTopBtn.hidden = NO;
        }else {
            self.backTopBtn.hidden = YES;
        }
    }
}
[图片上传中...([email protected])]

你可能感兴趣的:(置顶按钮(tableview))