iOS custom side slide button

After researching for a day, I just found a way to implement the delete button of a custom rounded left sliding cell, mainly to print out the view hierarchy of UITableView step by step (Xcode 9.0 iOS 10+)

In UITableView's ViewController's - (void)viewDidLayoutSubviews implementation.

iOS 11 (Xcode 9编译): UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton

The Delete button displayed by sliding is on _tableVeiw.subviews, print out _tableVeiw.subviews, there is a UISwipeActionStandardButton in it, take out this button is the delete button

UIButton *deleteButton = subview.subviews[0];

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

deleteButton.backgroundColor = [UIColor whiteColor];

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

At the same time to implement the agent

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

{

    self.editingIndexPath = indexPath;

    [self.view setNeedsLayout];   // 触发-(void)viewDidLayoutSubviews

}

 

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

{

    self.editingIndexPath = nil;

}

[self.view setNeedsLayout]; must be implemented

Here is all the code

- (void)viewDidLayoutSubviews

{

    [super viewDidLayoutSubviews];

    

    if (self.editingIndexPath)

    {

        [self configSwipeButtons];

    }

}

- (void)configSwipeButtons

{

    // Get the reference of the option button

    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(@"The view above%@",subview.subviews);

                // Reverse the order of buttons in 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];   // 触发-(void)viewDidLayoutSubviews

}

 

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

{

    self.editingIndexPath = nil;

}

- (void)configDeleteButton:(UIButton*)deleteButton

{

    if (deleteButton)

    {

   //method triggered by clicking the delete button

    }

}

 

- (void)configReadButton:(UIButton*)readButton

{

    if (readButton)

    {

 

    }

}

#pragma mark - return to edit mode, default is delete mode

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

{

    return UITableViewCellEditingStyleDelete;

}

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

    return @"delete";

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//Request data source submission to insert or delete the specified row receiver.

    if (editingStyle == UITableViewCellEditingStyleDelete) {//If the editing style is the delete style

 

    }else {

    }

}

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

{

    NSLog(@"Tapped accessory button");

}

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

    

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327079475&siteId=291194637