iOS11下自定義左滑刪除的處理.md

示例圖.jpg

在iOS11之前,如果需要自定義左滑刪除的顯示樣式,可以通過(guò)遍歷cell的子視圖,找到左滑顯示出來(lái)的視圖,直接更改該視圖即可

重寫自定義CelllayoutSubviews方法

// 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];
                    
                    // 移除標(biāo)題
                    for (UIView *view in btn.subviews) {
                        if ([view isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                            [view removeFromSuperview];
                            break;
                        }
                    }
                }
            }
        }
    }
}

但是在iOS11之后左滑刪除的視圖層級(jí)發(fā)生了變化。該視圖不再屬于Cell,而是屬于tableView。
所以再更改該樣式時(shí)需要在tableView上面來(lái)進(jìn)行更改。

視圖層級(jí)改變?yōu)?UITableView -> UISwipeActionPullView

所以更改該界面的方法如下


// ViewController.m

- (void)configSwipeButton
{
    // iOS 11層級(jí): 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];
        }
    }
}
注意點(diǎn)1

iOS11之前,對(duì)視圖的調(diào)整是在自定義的Cell中的調(diào)整
而iOS11之后是在tableView所在的ViewController中來(lái)調(diào)整

注意點(diǎn)2

上述方法中通過(guò)代碼畫出來(lái)一張和背景色相同的圖片,作為按鈕的高亮狀態(tài)的背景圖。

如果不進(jìn)行這步操作,左滑出來(lái)之后樣式是正常的,但是當(dāng)點(diǎn)擊時(shí)按鈕背景色會(huì)變?yōu)槟J(rèn)的紅色。所以需要設(shè)置其高亮狀態(tài)的背景圖片。

注意點(diǎn)3

UISwipeActionPullView該視圖只有在tableView處于編輯模式的時(shí)候才會(huì)出現(xiàn)。所以上面這個(gè)方法的調(diào)用時(shí)機(jī)應(yīng)該是在tableView處于左滑狀態(tài)時(shí),即編輯模式下。所以需要在tableView的進(jìn)入編輯模式的代理方法中進(jìn)行如下監(jiān)聽(tīng)處理

// 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];
    }
}

定義一個(gè)屬性用于判斷當(dāng)前是否處理編輯模式。用BOOL即可

當(dāng)tableView進(jìn)入編輯模式時(shí),將該屬性設(shè)置為YES,同時(shí)調(diào)用setNeedsLayout來(lái)進(jìn)行界面重繪。

界面重新繪制會(huì)進(jìn)入到viewDidLayoutSubviews方法中,在該方法內(nèi)檢測(cè)是否處于編輯狀態(tài)并且系統(tǒng)版本高于iOS11。

符合判斷后 重新調(diào)整左滑后的樣式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 寫在開頭:本文所介紹的方法使用的是iOS8-10中的API,不過(guò)同樣支持在iOS11上運(yùn)行。<最近更新時(shí)間:201...
    pika11閱讀 31,928評(píng)論 85 150
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,217評(píng)論 4 61
  • “果然是創(chuàng)業(yè)少女”打開微信一個(gè)好友評(píng)論了我剛發(fā)的狀態(tài),嗯,我在朋友圈賣甘蔗。 把這念頭告訴我媽的時(shí)候,...
    莫小粒閱讀 540評(píng)論 2 2
  • 何為最幸福?跛足者答:跑。盲人答:看見(jiàn)媽媽。癱瘓者答:站起來(lái)。聾人答:聽(tīng)見(jiàn)流行歌曲。哮喘病人答:別咳嗽。腎炎患者答...
    xcy無(wú)名閱讀 262評(píng)論 0 2
  • 月光灑在卑微的樟樹上 遠(yuǎn)方一位老奶奶 舉步艱難漫過(guò)蒼涼的夜 一輛黑色轎車奔馳而過(guò) 嚇壞了墜入愛(ài)河的小狗 燈光 星...
    向陽(yáng)貍閱讀 214評(píng)論 3 0

友情鏈接更多精彩內(nèi)容