Skip to content

Commit 12e8ae6

Browse files
committed
fix issue CEWendel#302: Right Buttons are covered iPhone X when landscape
1 parent dac8ea4 commit 12e8ae6

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

MGSwipeTableCell/MGSwipeTableCell.m

+37-3
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,15 @@ -(void) fixRegionAndAccesoryViews
677677
}
678678
}
679679

680+
-(UIEdgeInsets) getSafeInsets {
681+
if (@available(iOS 11, *)) {
682+
return self.safeAreaInsets;
683+
}
684+
else {
685+
return UIEdgeInsetsZero;
686+
}
687+
}
688+
680689
-(UIView *) swipeContentView
681690
{
682691
if (!_swipeContentView) {
@@ -700,12 +709,35 @@ -(void) layoutSubviews
700709
_swipeOverlay.frame = CGRectMake(0, 0, self.bounds.size.width, self.contentView.bounds.size.height);
701710
[self fixRegionAndAccesoryViews];
702711
if (_swipeView.image && !CGSizeEqualToSize(prevSize, _swipeOverlay.bounds.size)) {
712+
//refresh safeInsets in situations like layout change, orientation chage, table resize, etc.
713+
UIEdgeInsets safeInsets = [self getSafeInsets];
714+
if (safeInsets.left !=0 || safeInsets.right != 0) {
715+
if (_leftView) {
716+
[self changeFrameSafe:_leftView x: -_leftView.bounds.size.width - safeInsets.left];
717+
}
718+
if (_rightView) {
719+
[self changeFrameSafe:_rightView x: _swipeOverlay.bounds.size.width - safeInsets.right];
720+
}
721+
if (_swipeView) {
722+
[self changeFrameSafe:_swipeView x: -safeInsets.left];
723+
}
724+
}
703725
//refresh contentView in situations like layout change, orientation chage, table resize, etc.
704726
[self refreshContentView];
705727
}
706728
}
707729
}
708730

731+
732+
-(void) changeFrameSafe:(UIView *) view x:(CGFloat) x {
733+
CGRect frame = view.frame;
734+
CGAffineTransform transform = view.transform;
735+
view.transform = CGAffineTransformIdentity;
736+
frame.origin.x = x;
737+
view.frame = frame;
738+
view.transform = transform;
739+
}
740+
709741
-(void) fetchButtonsIfNeeded
710742
{
711743
if (_leftButtons.count == 0 && _delegate && [_delegate respondsToSelector:@selector(swipeTableCell:swipeButtonsForDirection:swipeSettings:expansionSettings:)]) {
@@ -718,13 +750,15 @@ -(void) fetchButtonsIfNeeded
718750

719751
-(void) createSwipeViewIfNeeded
720752
{
753+
UIEdgeInsets safeInsets = [self getSafeInsets];
721754
if (!_swipeOverlay) {
722755
_swipeOverlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.contentView.bounds.size.height)];
723756
[self fixRegionAndAccesoryViews];
724757
_swipeOverlay.hidden = YES;
725758
_swipeOverlay.backgroundColor = [self backgroundColorForSwipe];
726759
_swipeOverlay.layer.zPosition = 10; //force render on top of the contentView;
727-
_swipeView = [[UIImageView alloc] initWithFrame:_swipeOverlay.bounds];
760+
CGRect bounds = _swipeOverlay.bounds;
761+
_swipeView = [[UIImageView alloc] initWithFrame:CGRectMake(-safeInsets.left, bounds.origin.y, bounds.size.width, bounds.size.height)];
728762
_swipeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
729763
_swipeView.contentMode = UIViewContentModeCenter;
730764
_swipeView.clipsToBounds = YES;
@@ -736,14 +770,14 @@ -(void) createSwipeViewIfNeeded
736770
if (!_leftView && _leftButtons.count > 0) {
737771
_leftView = [[MGSwipeButtonsView alloc] initWithButtons:_leftButtons direction:MGSwipeDirectionLeftToRight differentWidth:_allowsButtonsWithDifferentWidth buttonsDistance:_leftSwipeSettings.buttonsDistance];
738772
_leftView.cell = self;
739-
_leftView.frame = CGRectMake(-_leftView.bounds.size.width, _leftSwipeSettings.topMargin, _leftView.bounds.size.width, _swipeOverlay.bounds.size.height - _leftSwipeSettings.topMargin - _leftSwipeSettings.bottomMargin);
773+
_leftView.frame = CGRectMake(-_leftView.bounds.size.width - safeInsets.left, _leftSwipeSettings.topMargin, _leftView.bounds.size.width, _swipeOverlay.bounds.size.height - _leftSwipeSettings.topMargin - _leftSwipeSettings.bottomMargin);
740774
_leftView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
741775
[_swipeOverlay addSubview:_leftView];
742776
}
743777
if (!_rightView && _rightButtons.count > 0) {
744778
_rightView = [[MGSwipeButtonsView alloc] initWithButtons:_rightButtons direction:MGSwipeDirectionRightToLeft differentWidth:_allowsButtonsWithDifferentWidth buttonsDistance:_rightSwipeSettings.buttonsDistance];
745779
_rightView.cell = self;
746-
_rightView.frame = CGRectMake(_swipeOverlay.bounds.size.width, _rightSwipeSettings.topMargin, _rightView.bounds.size.width, _swipeOverlay.bounds.size.height - _rightSwipeSettings.topMargin - _rightSwipeSettings.bottomMargin);
780+
_rightView.frame = CGRectMake(_swipeOverlay.bounds.size.width - safeInsets.right, _rightSwipeSettings.topMargin, _rightView.bounds.size.width, _swipeOverlay.bounds.size.height - _rightSwipeSettings.topMargin - _rightSwipeSettings.bottomMargin);
747781
_rightView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
748782
[_swipeOverlay addSubview:_rightView];
749783
}

0 commit comments

Comments
 (0)