Not Swift, but for anyone who lands here, looking for a solution ... I have good results with the following simple subclass:
@interface GSBTableViewRowAction : UITableViewRowAction @property UIImage *icon; @property UIFont *font; + (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title icon:(UIImage*)icon handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler; @end @implementation GSBTableViewRowAction + (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title icon:(UIImage*)icon handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler { if (title.length) title = [@"\n" stringByAppendingString:title]; // move title under centerline; icon will go above GSBTableViewRowAction *action = [super rowActionWithStyle:style title:title handler:handler]; action.icon = icon; return action; } - (void)_setButton:(UIButton*)button { if (self.font) button.titleLabel.font = self.font; if (self.icon) { [button setImage:[self.icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; button.tintColor = button.titleLabel.textColor; CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}]; button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height/2 + 5), 0, 0, -titleSize.width); // +5px gap under icon }
rowActionWithStyle:title:icon:handler is actually just a convenience method - the "secret sauce" overrides the (private?) method _setButton [credit for Jayesh for that!].
This will give you a badge centered by name or just a badge alone if you leave the heading null. Unfortunately, you cannot play with the title position using button.titleEdgeInsets , as you can with imageEdgeInsets , so I can best place the title below the center line (hence "\ n") with a small gap under the icon (5px above). The results look like

As a bonus, you can also change the font of the title to say something less by setting a new font property. for example, the above “add” action was performed using
GSBTableViewRowAction *add = [GSBTableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Add" icon:[UIImage imageNamed:@"Today-25"] handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ [self addToCalendar:self]; [tableView setEditing:NO animated:YES]; }]; add.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; add.backgroundColor = UIColor.orangeColor;
Caveat: _setButton is a private API, so YMMV ... We all hope that Apple will publish an open API to do what they apparantly do in their Mail.app [sic]
tiritea Sep 23 '16 at 10:23 2016-09-23 10:23
source share