美文网首页
需要的时候用:修改左滑出现的按钮的格式

需要的时候用:修改左滑出现的按钮的格式

作者: 西蜀 | 来源:发表于2017-12-05 09:45 被阅读134次

    table:

    - (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *titleContent = @"                            ";

    UITableViewRowAction *callAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleContent handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

    //        self.main_table.editing = NO;

    }];

    UITableViewRowAction *messageAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleContent handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

    //        self.main_table.editing = NO;

    }];

    return @[messageAction, callAction];

    }

    cell:

    // 这种方法是重写willTransitionToState这个方法,而不是重写layoutSubviews方法,因为在左滑菜单收起来的时候,背景图片会有闪一下的bug

    - (void)willTransitionToState:(UITableViewCellStateMask)state

    {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    [self setupSlideBtn];

    });

    }

    // 设置左滑菜单按钮的样式

    - (void)setupSlideBtn

    {

    // 判断系统是否是iOS11及以上版本

    if (@available(iOS 11.0, *)) {

    for (UIView *subView in self.superview.subviews) {

    if (![NSStringFromClass([subView class]) isEqualToString:@"UISwipeActionPullView"]) {

    continue;

    }

    for (UIView *buttonViews in subView.subviews) {

    if (![NSStringFromClass([buttonViews class]) isEqualToString:@"UISwipeActionStandardButton"]) {

    continue;

    }

    // 修改备注

    UIView *remarkContentView = subView.subviews[0];

    [self setupRowActionView:remarkContentView imageName:@"b27_icon_star_yellow"];

    // 删除

    UIView *deleteContentView = subView.subviews[1];

    [self setupRowActionView:deleteContentView imageName:@"b27_icon_star_gray"];

    }

    }

    } else {

    // iOS11以下做法

    for (UIView *subView in self.subviews) {

    if(![subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {

    continue;

    }

    // SMS短讯

    UIView *smsContentView = subView.subviews[0];

    smsContentView.backgroundColor = BlueColor;

    for (UIView *smsView in smsContentView.subviews) {

    //移除界面所有子视图  注意区分视图

    [smsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    UIImageView *smsImage = [[UIImageView alloc] init];

    smsImage.contentMode = UIViewContentModeScaleAspectFit;

    smsImage.image = [UIImage imageNamed:@"b27_icon_star_yellow"];

    CGFloat width = smsView.frame.size.width;

    smsImage.frame = CGRectMake((width - 25) / 2, -15, 25, 25);

    [smsView addSubview:smsImage];

    // 文字

    UILabel *sms = [self labelWithFrame:CGRectMake((width - 60) / 2, 40, 60, 20) text:@"哈哈哈哈哈" textColor:WhiteColor font:mainFontSize(14)];

    sms.textAlignment = NSTextAlignmentCenter;

    [smsView addSubview:sms];

    }

    // 讯息

    UIView *messageContentView = subView.subviews[1];

    messageContentView.backgroundColor = YellowColor;

    for (UIView *messageView in messageContentView.subviews) {

    [messageView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    UIImageView *messageImage = [[UIImageView alloc] init];

    messageImage.contentMode = UIViewContentModeScaleAspectFit;

    messageImage.image = [UIImage imageNamed:@"message_selected"];

    CGFloat width = messageView.frame.size.width;

    messageImage.frame = CGRectMake((width - 25) / 2, -15, 25, 25);

    [messageView addSubview:messageImage];

    // 文字

    UILabel *message = [self labelWithFrame:CGRectMake((width - 35) / 2, 40, 35, 20) text:@"噗噗噗噗" textColor:WhiteColor font:mainFontSize(14)];

    message.textAlignment = NSTextAlignmentCenter;

    [messageView addSubview:message];

    }

    }

    }

    }

    // 设置背景图片

    - (void)setupRowActionView:(UIView *)rowActionView imageName:(NSString *)imageName

    {

    rowActionView.backgroundColor = mainColor;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

    [rowActionView insertSubview:imageView atIndex:0];

    imageView.frame = CGRectMake(0, 0, 80, self.bounds.size.height);

    }

    -(UILabel*)labelWithFrame:(CGRect)rect text:(NSString *)text textColor:(UIColor*)color font:(UIFont*)font{

    UILabel *label = [[UILabel alloc] initWithFrame:rect] ;

    label.text = text;

    label.textColor = color;

    label.font = font;

    return label;

    }

    相关文章

      网友评论

          本文标题:需要的时候用:修改左滑出现的按钮的格式

          本文链接:https://www.haomeiwen.com/subject/mqsqixtx.html