自定義左滑編輯按鈕樣式,具體代碼編寫邏輯我就不寫了,網(wǎng)上很多文章都有介紹,我就貼下適配各個版本系統(tǒng)下圖層代碼和注意事項了。
一、觸發(fā)修改樣式的方法
1、控制器中使用viewDidLayoutSubviews
2、自定義TableView中使用layoutSubviews
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.editingIndexPath) {
[self csc_configTableViewSwipeButtons];
}
}
二、圖層代碼
- (void)csc_configTableViewSwipeButtons {
//XCode 10.2.1
//iOS 13層級: UITableView -> UISwipeActionPullView
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")) {
for (UIView *subview in self.tableView.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
for (UIView *subView_sub in subview.subviews) {
if ([subView_sub isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] &&
[subView_sub.subviews count] >= 1) {
subView_sub.backgroundColor = [UIColor grayBackgoundColor];
// 和iOS 10的按鈕順序相反
UIButton *deleteButton = subView_sub.subviews[0];
[self csc_configTableViewDeleteButton:deleteButton];
break;
}
}
}
}
}
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")) {
// iOS 11-12層級: UITableView -> UISwipeActionPullView
for (UIView *subview in self.tableView.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] &&
[subview.subviews count] >= 1) {
subview.backgroundColor = [UIColor grayBackgoundColor];
// 和iOS 10的按鈕順序相反
UIButton *deleteButton = subview.subviews[0];
[self csc_configTableViewDeleteButton:deleteButton];
break;
}
}
}
else {
// iOS 8-10層級: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
YDCouponTableViewCell *tableCell = [self.tableView cellForRowAtIndexPath:self.editingIndexPath];
for (UIView *subview in tableCell.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] &&
[subview.subviews count] >= 1) {
subview.backgroundColor = [UIColor grayBackgoundColor];
UIButton *deleteButton = subview.subviews[0];
[self csc_configTableViewDeleteButton:deleteButton];
break;
}
}
}
}
方法在一里寫的兩個方法中調(diào)用。
SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO是我寫的宏判斷系統(tǒng)的
csc_configTableViewDeleteButton寫配置樣式就好。
三、UITableVIew Delegate方法
在iOS13以前,只需要這樣寫
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
self.editingIndexPath = indexPath;
//這里會觸發(fā)viewDidLayoutSubviews方法,從而執(zhí)行修改樣式代碼
[self.view setNeedsLayout];
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(nullable NSIndexPath *)indexPath {
self.editingIndexPath = nil;
}
在iOS13中,同一個cell多次左右滑動只會調(diào)用一次willBeginEditingRowAtIndexPath,就出現(xiàn)了第一次樣式是對的,后面的就沒了。
于是我加了下面一段代碼
- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//系統(tǒng)大于13
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")) {
self.editingIndexPath = indexPath;
[self.view setNeedsLayout];
}
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[tableView setEditing:NO animated:YES];
}];
return @[deleteAction];
}
目前沒什么問題,就先這樣吧!
有時間了再研究研究。