Skip to content

Commit

Permalink
fix issue MortimerGoro#302: Right Buttons are covered iPhone X when l…
Browse files Browse the repository at this point in the history
…andscape
  • Loading branch information
MortimerGoro committed Nov 10, 2017
1 parent dac8ea4 commit 12e8ae6
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions MGSwipeTableCell/MGSwipeTableCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,15 @@ -(void) fixRegionAndAccesoryViews
}
}

-(UIEdgeInsets) getSafeInsets {
if (@available(iOS 11, *)) {
return self.safeAreaInsets;
}
else {
return UIEdgeInsetsZero;
}
}

-(UIView *) swipeContentView
{
if (!_swipeContentView) {
Expand All @@ -700,12 +709,35 @@ -(void) layoutSubviews
_swipeOverlay.frame = CGRectMake(0, 0, self.bounds.size.width, self.contentView.bounds.size.height);
[self fixRegionAndAccesoryViews];
if (_swipeView.image && !CGSizeEqualToSize(prevSize, _swipeOverlay.bounds.size)) {
//refresh safeInsets in situations like layout change, orientation chage, table resize, etc.
UIEdgeInsets safeInsets = [self getSafeInsets];
if (safeInsets.left !=0 || safeInsets.right != 0) {
if (_leftView) {
[self changeFrameSafe:_leftView x: -_leftView.bounds.size.width - safeInsets.left];
}
if (_rightView) {
[self changeFrameSafe:_rightView x: _swipeOverlay.bounds.size.width - safeInsets.right];
}
if (_swipeView) {
[self changeFrameSafe:_swipeView x: -safeInsets.left];
}
}
//refresh contentView in situations like layout change, orientation chage, table resize, etc.
[self refreshContentView];
}
}
}


-(void) changeFrameSafe:(UIView *) view x:(CGFloat) x {
CGRect frame = view.frame;
CGAffineTransform transform = view.transform;
view.transform = CGAffineTransformIdentity;
frame.origin.x = x;
view.frame = frame;
view.transform = transform;
}

-(void) fetchButtonsIfNeeded
{
if (_leftButtons.count == 0 && _delegate && [_delegate respondsToSelector:@selector(swipeTableCell:swipeButtonsForDirection:swipeSettings:expansionSettings:)]) {
Expand All @@ -718,13 +750,15 @@ -(void) fetchButtonsIfNeeded

-(void) createSwipeViewIfNeeded
{
UIEdgeInsets safeInsets = [self getSafeInsets];
if (!_swipeOverlay) {
_swipeOverlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.contentView.bounds.size.height)];
[self fixRegionAndAccesoryViews];
_swipeOverlay.hidden = YES;
_swipeOverlay.backgroundColor = [self backgroundColorForSwipe];
_swipeOverlay.layer.zPosition = 10; //force render on top of the contentView;
_swipeView = [[UIImageView alloc] initWithFrame:_swipeOverlay.bounds];
CGRect bounds = _swipeOverlay.bounds;
_swipeView = [[UIImageView alloc] initWithFrame:CGRectMake(-safeInsets.left, bounds.origin.y, bounds.size.width, bounds.size.height)];
_swipeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_swipeView.contentMode = UIViewContentModeCenter;
_swipeView.clipsToBounds = YES;
Expand All @@ -736,14 +770,14 @@ -(void) createSwipeViewIfNeeded
if (!_leftView && _leftButtons.count > 0) {
_leftView = [[MGSwipeButtonsView alloc] initWithButtons:_leftButtons direction:MGSwipeDirectionLeftToRight differentWidth:_allowsButtonsWithDifferentWidth buttonsDistance:_leftSwipeSettings.buttonsDistance];
_leftView.cell = self;
_leftView.frame = CGRectMake(-_leftView.bounds.size.width, _leftSwipeSettings.topMargin, _leftView.bounds.size.width, _swipeOverlay.bounds.size.height - _leftSwipeSettings.topMargin - _leftSwipeSettings.bottomMargin);
_leftView.frame = CGRectMake(-_leftView.bounds.size.width - safeInsets.left, _leftSwipeSettings.topMargin, _leftView.bounds.size.width, _swipeOverlay.bounds.size.height - _leftSwipeSettings.topMargin - _leftSwipeSettings.bottomMargin);
_leftView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[_swipeOverlay addSubview:_leftView];
}
if (!_rightView && _rightButtons.count > 0) {
_rightView = [[MGSwipeButtonsView alloc] initWithButtons:_rightButtons direction:MGSwipeDirectionRightToLeft differentWidth:_allowsButtonsWithDifferentWidth buttonsDistance:_rightSwipeSettings.buttonsDistance];
_rightView.cell = self;
_rightView.frame = CGRectMake(_swipeOverlay.bounds.size.width, _rightSwipeSettings.topMargin, _rightView.bounds.size.width, _swipeOverlay.bounds.size.height - _rightSwipeSettings.topMargin - _rightSwipeSettings.bottomMargin);
_rightView.frame = CGRectMake(_swipeOverlay.bounds.size.width - safeInsets.right, _rightSwipeSettings.topMargin, _rightView.bounds.size.width, _swipeOverlay.bounds.size.height - _rightSwipeSettings.topMargin - _rightSwipeSettings.bottomMargin);
_rightView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[_swipeOverlay addSubview:_rightView];
}
Expand Down

0 comments on commit 12e8ae6

Please sign in to comment.