Copyright ? 2017年ZaneWangWang. All rights reserved.
? ? ? ?最近設計給了這樣一張圖,cell的刪除按鈕是一張圖片而不再是刪除的文字.如果過自定義cell的這些功能會非常麻煩,因此,我在網(wǎng)上了解到UITableViewCellDeleteConfirmationView這樣一個類,在他的基礎之上來自定義cell的刪除按鈕樣式.具體方案如下:(如果過你看到的不是原文請點擊查看原文)
1.首先要找到UITableViewCellDeleteConfirmationView,我們要知道它是cell的子視圖,因此我們遍歷cell的子視圖,判斷如果遍歷到的view是UITableViewCellDeleteConfirmationView,我們在遍歷UITableViewCellDeleteConfirmationView對象的子視圖找到刪除按鈕,具體代碼如下:
注意:這個方法是寫在自定義cell里的,并且調(diào)用的時候必須在layoutSubviews方法里邊才能找到UITableViewCellDeleteConfirmationView
- (void)customDeleteStyle{
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull deleteConfirmationView, NSUInteger idx, BOOL * _Nonnull stop) {
if ([deleteConfirmationView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
deleteConfirmationView.backgroundColor = [UIColor clearColor];//這里是需要的因為入過不設置為透明,我們接下來做的想過會有一個系統(tǒng)刪除按鈕的紅色方框
[deleteConfirmationView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull deleteBtn, NSUInteger idx, BOOL * _Nonnull stop) {
deleteBtn.backgroundColor = [UIColor clearColor];
if ([deleteBtn isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)deleteBtn;
self.deleteBtn = btn;
//找到刪除按鈕以后調(diào)用下邊的方法在按鈕上添加想要的效果
[self addCustomSubLayerWithDeleteBtn:btn];
}}];}}];}
2.找到刪除按鈕以后調(diào)用下邊的方法在按鈕上添加想要的效果,代碼如下:
- (void)addCustomSubLayerWithDeleteBtn:(UIButton *)btn{
CALayer *radiusLayer = [CALayer layer];
radiusLayer.frame = CGRectMake(btn.width*0.125f, btn.height*0.05f, btn.width*0.75f, btn.height*0.9f);
radiusLayer.cornerRadius = 8.0f;
radiusLayer.backgroundColor = [UIColor redColor].CGColor;
[btn.layer addSublayer:radiusLayer];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(radiusLayer.width*0.25f, (radiusLayer.height - radiusLayer.width*0.5f)/2.0f, radiusLayer.width*0.5f, radiusLayer.width*0.5f);
imageLayer.contents = (id)[UIImage imageNamed:@"delete"].CGImage;
[radiusLayer addSublayer:imageLayer];
btn.clipsToBounds = YES;
}
到這里自定義的樣式已經(jīng)出來了,基本上你已經(jīng)可以實現(xiàn)自己想要的效果了,根據(jù)自己想要的細節(jié)效果再作進一步的處理.
完成之后的測試效果如下: