iOS 自定義側(cè)滑按鈕

圖片發(fā)自簡書App


研究了有一天了秉颗,剛找到方法去實現(xiàn)自定義圓角左滑cell的刪除按鈕吩蔑,主要是一步一步打印出UITableView的視圖層級(Xcode 9.0 iOS 10+)

在UITableView的ViewController的- (void)viewDidLayoutSubviews實現(xiàn)应民。

iOS 11 (Xcode 9編譯): UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton

滑動展示的Delete按鈕在_tableVeiw.subviews上始绍,打印出_tableVeiw.subviews胜茧,里面有一個UISwipeActionStandardButton室叉,取出這個按鈕就是刪除按鈕

UIButton *deleteButton = subview.subviews[0];

deleteButton.frame = CGRectMake(0, 5, 70, 110);

deleteButton.backgroundColor = [UIColor whiteColor];

? ? ? ? ? ? ? ? [deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];

同時要實現(xiàn)代理

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? self.editingIndexPath = indexPath;

? ? [self.view setNeedsLayout];? // 觸發(fā)-(void)viewDidLayoutSubviews

}


- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? self.editingIndexPath = nil;

}

[self.view setNeedsLayout];必須要實現(xiàn)

以下是全部代碼

- (void)viewDidLayoutSubviews

{

? ? [super viewDidLayoutSubviews];


? ? if (self.editingIndexPath)

? ? {

? ? ? ? [self configSwipeButtons];

? ? }

}

- (void)configSwipeButtons

{

? ? // 獲取選項按鈕的reference

? ? if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")){

? ? ? ? for (UIView *subview in _tableVeiw.subviews)

? ? ? ? {

? ? ? ? ? ? subview.backgroundColor = [UIColor whiteColor];

? ? ? ? ? ? if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? NSLog(@"上面的視圖 %@",subview.subviews);

? ? ? ? ? ? ? ? // 和iOS 10的按鈕順序相反

? ? ? ? ? ? ? ? UIButton *deleteButton = subview.subviews[0];

? ? ? ? ? ? ? ? deleteButton.frame = CGRectMake(0, 5, 70, 110);

//? ? ? ? ? ? ? ? deleteButton.layer.cornerRadius = 10;

//? ? ? ? ? ? ? ? deleteButton.layer.masksToBounds = YES;

? ? ? ? ? ? ? ? deleteButton.backgroundColor = [UIColor whiteColor];

? ? ? ? ? ? ? ? [deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];

? ? ? ? ? ? ? ? [self configDeleteButton:deleteButton];

? ? ? ? ? ? ? ? [self centerImageAndTextOnButton:deleteButton];

? ? ? ? ? ? }

? ? ? ? }

? ? }else{

? ? ? ? // iOS 8-10層級: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView

? ? ? ? NoticeTableVC *tableCell = [_tableVeiw cellForRowAtIndexPath:self.editingIndexPath];

? ? ? ? for (UIView *subview in tableCell.subviews)

? ? ? ? {

? ? ? ? ? ? subview.backgroundColor = [UIColor whiteColor];

? ? ? ? ? ? if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? UIButton *deleteButton = subview.subviews[0];


? ? ? ? ? ? ? ? [self configDeleteButton:deleteButton];

? ? ? ? ? ? ? ? deleteButton.frame = CGRectMake(0, 5, 60, 110);

? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? deleteButton.layer.cornerRadius = 10;

? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? deleteButton.layer.masksToBounds = YES;

? ? ? ? ? ? ? ? deleteButton.backgroundColor = [UIColor whiteColor];

? ? ? ? ? ? ? ? [deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];

? ? ? ? ? ? ? ? [self configDeleteButton:deleteButton];

? ? ? ? ? ? ? ? [self centerImageAndTextOnButton:deleteButton];

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

- (void)centerImageAndTextOnButton:(UIButton*)button

{

? ? if (SYSTEM_VERSION_LESS_THAN(@"11.0"))

? ? {

? ? ? ? CGRect btnFrame = button.frame;

? ? ? ? btnFrame.origin.y = 30;

? ? ? ? button.frame = btnFrame;

? ? }

}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? self.editingIndexPath = indexPath;

? ? [self.view setNeedsLayout];? // 觸發(fā)-(void)viewDidLayoutSubviews

}


- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? self.editingIndexPath = nil;

}

- (void)configDeleteButton:(UIButton*)deleteButton

{

? ? if (deleteButton)

? ? {

? //點擊刪除按鈕觸發(fā)的方法

? ? }

}


- (void)configReadButton:(UIButton*)readButton

{

? ? if (readButton)

? ? {


? ? }

}

#pragma mark - 返回編輯模式,默認(rèn)為刪除模式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? return UITableViewCellEditingStyleDelete;

}

- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

? ? return @"刪除";

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//請求數(shù)據(jù)源提交的插入或刪除指定行接收者卵佛。

? ? if (editingStyle == UITableViewCellEditingStyleDelete) {//如果編輯樣式為刪除樣式


? ? }else {

? ? }

}

-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

? ? NSLog(@"Tapped accessory button");

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


}

項目demo地址:https://gitee.com/1023678795/LeftSliderDemo.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末杨赤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子截汪,更是在濱河造成了極大的恐慌疾牲,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挫鸽,死亡現(xiàn)場離奇詭異说敏,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)丢郊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來医咨,“玉大人枫匾,你說我怎么就攤上這事∧饣矗” “怎么了干茉?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長很泊。 經(jīng)常有香客問我角虫,道長,這世上最難降的妖魔是什么委造? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任戳鹅,我火速辦了婚禮,結(jié)果婚禮上昏兆,老公的妹妹穿的比我還像新娘枫虏。我一直安慰自己,他們只是感情好爬虱,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布隶债。 她就那樣靜靜地躺著,像睡著了一般跑筝。 火紅的嫁衣襯著肌膚如雪死讹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天曲梗,我揣著相機(jī)與錄音赞警,去河邊找鬼逛腿。 笑死,一個胖子當(dāng)著我的面吹牛仅颇,可吹牛的內(nèi)容都是我干的单默。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼忘瓦,長吁一口氣:“原來是場噩夢啊……” “哼搁廓!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起耕皮,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤境蜕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后凌停,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體粱年,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年罚拟,在試婚紗的時候發(fā)現(xiàn)自己被綠了台诗。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡赐俗,死狀恐怖拉队,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情阻逮,我是刑警寧澤粱快,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站叔扼,受9級特大地震影響事哭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瓜富,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一鳍咱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧食呻,春花似錦流炕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至干旧,卻和暖如春渠欺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背椎眯。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工挠将, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留胳岂,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓舔稀,卻偏偏與公主長得像乳丰,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子内贮,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容