How can I create a big, red UIButton with iOS?

后端 未结 6 1296
一整个雨季
一整个雨季 2020-11-29 15:03

Using iOS, how would I go about creating a red \"delete\" button similar to the one used when deleting contacts on the iPhone?

6条回答
  •  忘掉有多难
    2020-11-29 15:29

    I would like to contribute a solution which does not use images but which gives the same look as the 'delete' button in Contacts. In the below example I will use assume a UITableView with grouped, static cells, designed in storyboard. Make one of the sections have only a single cell. That cell will be the 'delete' button. Give the cell a red background color (f.e. Red 221, Green 0, Blue 2)

    What we will do is add two GradientLayers to the cell. The first will cover the upper half of the cell. The second will cover the lower half.

    Add QuartzCore to your implementation file:

    #import 
    

    Start with making an outlet to this cell:

    @property (strong, nonatomic) IBOutlet UITableViewCell *cellDelete;
    

    Create a method in which the cell will be formatted:

    - (void)formatCellDelete
    {
        // Top Gradient //
        CAGradientLayer *gradientTop = [CAGradientLayer layer];
    
        // Make a frame for the layer based on the size of the cells contentView
        // Make it half the height
        // The width will be -20 so the gradient will not overflow
        CGRect frame = CGRectMake(0, 0, _cellDelete.contentView.frame.size.width - 20, _cellDelete.contentView.frame.size.height / 2);
        gradientTop.frame = frame;
    
        gradientTop.cornerRadius = 8;
    
        UIColor* topColor = [UIColor colorWithWhite:1.0f alpha:0.75f];
        UIColor* bottomColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
        gradientTop.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
    
        [_cellDelete.contentView.layer setMasksToBounds:YES];
        [_cellDelete.contentView.layer insertSublayer:gradientTop atIndex:0];
    
    
    
        // Bottom Gradient //
        CAGradientLayer *gradientBottom = [CAGradientLayer layer];
    
        // Make a frame for the layer based on the size of the cells contentView
        // Make it half the height
        // The width will be -20 so the gradient will not overflow
        frame = CGRectMake(0, 0, _cellDelete.contentView.frame.size.width - 20, _cellDelete.contentView.frame.size.height / 2);
        // And move to bottom
        frame.origin.y = frame.size.height;
        gradientBottom.frame = frame;
    
        topColor = [UIColor colorWithWhite:0.0f alpha:0.05f]; //0.20
        bottomColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
        gradientBottom.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
    
        [_cellDelete.contentView.layer setMasksToBounds:YES];
        [_cellDelete.contentView.layer insertSublayer:gradientBottom atIndex:0];
    
    
        // Define a selected-backgroundColor so the cell changes color when the user tabs it
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:[UIColor colorWithRed:(float)(0.502) green:0.0 blue:0.0 alpha:1.000]];
        bgColorView.layer.cornerRadius = 10;
        [_cellDelete setSelectedBackgroundView:bgColorView];
    }
    

    The above will give your cell the glass-look like the 'delete' button in Contacts. But we also want it to change color when the user taps it. This is what the last piece of code in the above method will do. It will set a different view with a darker color as the selectedBackgroundView.

    After tapping the cell will stay selected and will keep its dark color. To automatically deselect the cell we do the following:

    Start with a constant which defines the section nr of your delete cell:

    static NSInteger const SECTION_DELETE = 1;
    

    Now implement the (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method (defined in UITableViewDelegate):

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        if(indexPath.section == SECTION_DELETE){
    
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    
    
        // Navigation logic may go here. Create and push another view controller.
        /*
          *detailViewController = [[ alloc] initWithNibName:@"" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         */
    
    }
    

提交回复
热议问题