示例圖.jpg
在iOS11之前,如果需要自定義左滑刪除的顯示樣式,可以通過遍歷cell的子視圖,找到左滑顯示出來的視圖,直接更改該視圖即可
重寫自定義Cell的layoutSubviews
方法
// TableViewCell.m
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
subView.backgroundColor = [UIColor colorWithHEXString:@"#F2F2F4"];
for (UIView *btnView in subView.subviews) {
if ([btnView isKindOfClass:NSClassFromString(@"_UITableViewCellActionButton")]) {
UIButton *btn = (UIButton *)btnView;
btn.bounds = CGRectMake(0, 0, 50, 50);
btn.backgroundColor = [UIColor whiteColor];
btn.layer.cornerRadius = 25;
[btn setBackgroundImage:[UIImage imageNamed:@"collection_item_delete"] forState:UIControlStateNormal];
// 移除標題
for (UIView *view in btn.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
[view removeFromSuperview];
break;
}
}
}
}
}
}
}
但是在iOS11之后左滑刪除的視圖層級發生了變化。該視圖不再屬于Cell,而是屬于tableView。
所以再更改該樣式時需要在tableView上面來進行更改。
視圖層級改變為 UITableView -> UISwipeActionPullView
所以更改該界面的方法如下
// ViewController.m
- (void)configSwipeButton
{
// iOS 11層級: UITableView -> UISwipeActionPullView
for (UIView *subview in self.tableView.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
{
subview.backgroundColor = [UIColor colorWithHEXString:@"F2F2F4"];
UIButton *deleteBtn = subview.subviews.firstObject;
[deleteBtn setImage:[UIImage imageNamed:@"collection_item_delete"] forState:UIControlStateNormal];
deleteBtn.backgroundColor = [UIColor colorWithHEXString:@"F2F2F4"];
[deleteBtn setTitle:@"" forState:UIControlStateNormal];
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor color5] CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[deleteBtn setBackgroundImage:theImage forState:UIControlStateHighlighted];
[deleteBtn setBackgroundImage:theImage forState:UIControlStateNormal];
}
}
}
注意點1
iOS11之前,對視圖的調整是在自定義的Cell中的調整
而iOS11之后是在tableView所在的ViewController中來調整
注意點2
上述方法中通過代碼畫出來一張和背景色相同的圖片,作為按鈕的高亮狀態的背景圖。
如果不進行這步操作,左滑出來之后樣式是正常的,但是當點擊時按鈕背景色會變為默認的紅色。所以需要設置其高亮狀態的背景圖片。
注意點3
UISwipeActionPullView
該視圖只有在tableView處于編輯模式的時候才會出現。所以上面這個方法的調用時機應該是在tableView
處于左滑狀態時,即編輯模式下。所以需要在tableView的進入編輯模式的代理方法中進行如下監聽處理
// ViewController.m
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
self.editingIndexPath = indexPath;
[self.view setNeedsLayout];
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
self.editingIndexPath = nil;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.editingIndexPath && ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)) {
[self configSwipeButton];
}
}
定義一個屬性用于判斷當前是否處理編輯模式。用BOOL
即可
當tableView進入編輯模式時,將該屬性設置為YES,同時調用setNeedsLayout
來進行界面重繪。
界面重新繪制會進入到viewDidLayoutSubviews
方法中,在該方法內檢測是否處于編輯狀態并且系統版本高于iOS11。
符合判斷后 重新調整左滑后的樣式