Skip to content

Commit 99345e2

Browse files
committed
Implemented allowsButtonsWithDifferentSize property: Controls whether buttons with different width are allowed. Buttons are resized to have the same size by default.
1 parent c0928ab commit 99345e2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

MGSwipeTableCell/MGSwipeTableCell.h

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ typedef NS_ENUM(NSInteger, MGSwipeState) {
145145

146146
// default is NO. Controls whether multiple cells can be swipped simultaneously
147147
@property (nonatomic) BOOL allowsMultipleSwipe;
148+
// default is NO. Controls whether buttons with different width are allowed. Buttons are resized to have the same size by default.
149+
@property (nonatomic) BOOL allowsButtonsWithDifferentWidth;
148150

149151
/** Optional background color for swipe overlay. If not set, its inferred automatically from the cell contentView */
150152
@property (nonatomic, strong) UIColor * swipeBackgroundColor;

MGSwipeTableCell/MGSwipeTableCell.m

+13-5
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,21 @@ @implementation MGSwipeButtonsView
5454

5555
#pragma mark Layout
5656

57-
-(instancetype) initWithButtons:(NSArray*) buttonsArray direction:(MGSwipeDirection) direction
57+
-(instancetype) initWithButtons:(NSArray*) buttonsArray direction:(MGSwipeDirection) direction differentWidth:(BOOL) differentWidth
5858
{
59+
CGFloat containerWidth = 0;
5960
CGSize maxSize = CGSizeZero;
6061

6162
for (UIView * button in buttonsArray) {
62-
maxSize.width += button.bounds.size.width;
63+
containerWidth += button.bounds.size.width;
64+
maxSize.width = MAX(maxSize.width, button.bounds.size.width);
6365
maxSize.height = MAX(maxSize.height, button.bounds.size.height);
6466
}
67+
if (!differentWidth) {
68+
containerWidth = maxSize.width * buttonsArray.count;
69+
}
6570

66-
if (self = [super initWithFrame:CGRectMake(0, 0, maxSize.width, maxSize.height)]) {
71+
if (self = [super initWithFrame:CGRectMake(0, 0, containerWidth, maxSize.height)]) {
6772
_fromLeft = direction == MGSwipeDirectionLeftToRight;
6873
_container = [[UIView alloc] initWithFrame:self.bounds];
6974
_container.clipsToBounds = YES;
@@ -74,6 +79,9 @@ -(instancetype) initWithButtons:(NSArray*) buttonsArray direction:(MGSwipeDirect
7479
if ([button isKindOfClass:[UIButton class]]) {
7580
[(UIButton *)button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
7681
}
82+
if (!differentWidth) {
83+
button.frame = CGRectMake(0, 0, maxSize.width, maxSize.height);
84+
}
7785
button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
7886
[_container insertSubview:button atIndex: _fromLeft ? 0: _container.subviews.count];
7987
}
@@ -522,14 +530,14 @@ -(void) createSwipeViewIfNeeded
522530

523531
[self fetchButtonsIfNeeded];
524532
if (!_leftView && _leftButtons.count > 0) {
525-
_leftView = [[MGSwipeButtonsView alloc] initWithButtons:_leftButtons direction:MGSwipeDirectionLeftToRight];
533+
_leftView = [[MGSwipeButtonsView alloc] initWithButtons:_leftButtons direction:MGSwipeDirectionLeftToRight differentWidth:_allowsButtonsWithDifferentWidth];
526534
_leftView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
527535
_leftView.cell = self;
528536
_leftView.frame = CGRectMake(-_leftView.bounds.size.width, 0, _leftView.bounds.size.width, _swipeOverlay.bounds.size.height);
529537
[_swipeOverlay addSubview:_leftView];
530538
}
531539
if (!_rightView && _rightButtons.count > 0) {
532-
_rightView = [[MGSwipeButtonsView alloc] initWithButtons:_rightButtons direction:MGSwipeDirectionRightToLeft];
540+
_rightView = [[MGSwipeButtonsView alloc] initWithButtons:_rightButtons direction:MGSwipeDirectionRightToLeft differentWidth:_allowsButtonsWithDifferentWidth];
533541
_rightView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
534542
_rightView.cell = self;
535543
_rightView.frame = CGRectMake(_swipeOverlay.bounds.size.width, 0, _rightView.bounds.size.width, _swipeOverlay.bounds.size.height);

0 commit comments

Comments
 (0)