最近iOS 11 版本發(fā)布其屏,iOS 11就不吐槽了喇勋,畢竟每次更新系統(tǒng)都會(huì)有一大堆的適配。偎行。川背。每年一次的煎熬。睦优。渗常。
下面就來說說這次的 iOS 11 tableView 中cell 自定義樣式適配 遇到的坑
iOS 11中cell左滑后,右滑不會(huì)再將cell恢復(fù)至原始狀態(tài)汗盘,需要點(diǎn)擊才可以恢復(fù)皱碘。
在iOS 11以前,我們知道更改cell 的滑動(dòng)刪除的樣式隐孽,可以從 layoutSubviews 中修改
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView subView in self.subviews)
{
if (subView.frame.orgin.x) >=self.frame.size.width)
{
for (UIView childView in subView.subviews)
{
if ([childView isKindOfClass:[UIButton class]])
{
UIButton delBtn = (UIButton )childView;
[delBtn setBackgroundColor:[UIColor orangeColor]];
[delBtn setTitleColor:[UIColor purpleColor] forState:(UIControlStateNormal)];
break;
}
}
}
}
}
或者是通過willTransitionToState 方法來進(jìn)行更改癌椿,這里要說一下健蕊,這個(gè)方法優(yōu)于上面的方法,因?yàn)檫@里是當(dāng)滑動(dòng)的時(shí)候才會(huì)去更新cell的刪除樣式踢俄,性能優(yōu)化之路艱辛 缩功,改一點(diǎn)是一點(diǎn)
- (void)willTransitionToState:(UITableViewCellStateMask)state {
NSLog(@"%s, %d", __FUNCTION__, __LINE__);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (UIView *subView in self.subviews) {
NSLog(@"%----@%----", NSStringFromClass([subView class]) );
if ([NSStringFromClass([subView class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
NSLog(@"開始加載啦!6及臁5招俊!A斩ぁJ颇尽!8枥痢@沧馈!<霸怼8δ小!Q樯铡0宀怠!T刖健K褡Pā>蠹唷!>省:葡啊!<们稹谱秽!");
UIButton *view = ((UIButton *)[subView.subviews firstObject]);
view.backgroundColor = [UIColor clearColor];
view.superview.backgroundColor = [UIColor clearColor];
NSLog(@"%@", view.subviews[0]);
UIView *backImageView = [[UIView alloc] init];
// 這一塊的frame
backImageView.frame = CGRectMake(5, 10, 60, 150 - 20);
backImageView.backgroundColor = [UIColor redColor];
backImageView.layer.masksToBounds = YES;
backImageView.layer.cornerRadius = 10;
[view addSubview:backImageView];
UIImageView *iamgeView = [[UIImageView alloc] init];
[view addSubview:iamgeView];
iamgeView.frame = CGRectMake(15, 40, 36, 36);
iamgeView.image = [UIImage imageNamed:@"刪除cell"];
UILabel *label = [[UILabel alloc] init];
label.text = view.titleLabel.text;
label.frame = CGRectMake(0, 90, 66, 21);
[view addSubview:label];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
[view setTitle:@"" forState:(UIControlStateNormal)];
}
}
});
}
上面的兩種方式都是iOS 11以前可以生效的方法,但是在進(jìn)入iOS11 之后摹迷,刪除樣式的cell 層次有改變疟赊,目前在我項(xiàng)目里面用的是第二種,所以這次也主要是針對(duì)第二種方式的改進(jìn)峡碉,在通過層次圖近哟,我們發(fā)現(xiàn)
(這里紅色區(qū)域及以上的控件是自己加上的,這里是選擇了覆蓋之前的樣式)
先貼一下代碼
if ((([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)?(YES):(NO))) {
for (UIView *subView in self.superview.subviews) {
if ([NSStringFromClass([subView class]) isEqualToString:@"UISwipeActionPullView"]) {
UIView *suView = (UIView *)(subView.subviews[0].subviews[0]);
suView.backgroundColor = [UIColor clearColor];
UIButton *view = ((UIButton *)[subView.subviews firstObject]);
view.backgroundColor = [UIColor clearColor];
view.superview.backgroundColor = [UIColor clearColor];
NSLog(@"%@", view.subviews[0]);
UIView *backImageView = [[UIView alloc] init];
// 這一塊的frame
backImageView.frame = CGRectMake(5, 10, 60, 150 - 20);
backImageView.backgroundColor = [UIColor redColor];
backImageView.layer.masksToBounds = YES;
backImageView.layer.cornerRadius = 10;
[view addSubview:backImageView];
UIImageView *iamgeView = [[UIImageView alloc] init];
[view addSubview:iamgeView];
iamgeView.frame = CGRectMake(15, 40, 36, 36);
iamgeView.image = [UIImage imageNamed:@"刪除cell"];
UILabel *label = [[UILabel alloc] init];
label.text = view.titleLabel.text;
label.frame = CGRectMake(0, 90, 66, 21);
[view addSubview:label];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
[view setTitle:@"" forState:(UIControlStateNormal)];
}
}
}
這里是先通過判斷系統(tǒng)是否是iOS 11鲫寄,因?yàn)橹暗拇a在iOS 11之前都是可用的吉执,所以走了分支疯淫,最后我會(huì)把完整的代碼貼出來
通過控制臺(tái)和層次圖,我們找到了一個(gè)UISwipeActionPullView 這個(gè)類戳玫,通過觀察熙掺,我們知道UISwipeActionPullView 這個(gè)類的父類就是 uitableViewWrapperView,我們的刪除樣式就在這個(gè)子集里面咕宿,那么就可以繼續(xù)往下走了
一步步找他的子集币绩,因?yàn)槲覀円业阶罱K顯示的刪除按鈕,并進(jìn)行改造
這個(gè)uibutton的子類府阀,就是我們要找的按鈕了@嗬恕!<∷啤费就!再看看這個(gè)button子類里面有什么
一個(gè)view,imageView川队,buttonLabel力细,標(biāo)準(zhǔn)的button,從buttonLabel的text中也可以看出固额,他就是我們要找的刪除樣式
因?yàn)槲业捻?xiàng)目中采用的是自定義的樣式眠蚂,所以這里我將他們的顏色都清除掉了,最終就只顯示我需要的樣式了斗躏。
好逝慧,完整的類在我的GitHub里面,覺得好用可以給star哦W牟凇5殉肌!
https://github.com/bommmmmmm/tableViewCustomDeletCell.git
===============================更新 ==========================
在iOS 11中隧饼,左滑cell時(shí)沈堡,有時(shí)不會(huì)觸發(fā)willTransitionToState ,可能是系統(tǒng)原因燕雁,暫未解決诞丽。