Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize for ios14 #434

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SWTableViewCell/PodFiles/SWTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef NS_ENUM(NSInteger, SWCellState)
@property (nonatomic, copy) NSArray *rightUtilityButtons;

@property (nonatomic, weak) id <SWTableViewCellDelegate> delegate;
@property (nonatomic, strong) UIView *contentCellView;

- (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons WithButtonWidth:(CGFloat) width;
- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons WithButtonWidth:(CGFloat) width;
Expand Down
49 changes: 24 additions & 25 deletions SWTableViewCell/PodFiles/SWTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#import "SWTableViewCell.h"
#import "SWUtilityButtonView.h"

static NSString * const kTableViewCellContentView = @"UITableViewCellContentView";

#define kSectionIndexWidth 15
#define kAccessoryTrailingSpace 15
#define kLongPressMinimumDuration 0.16f
Expand Down Expand Up @@ -47,7 +45,7 @@ @implementation SWTableViewCell {
UIView *_contentCellView;
BOOL layoutUpdating;
}

@synthesize contentCellView = _contentCellView;
#pragma mark Initializers

- (instancetype)initWithCoder:(NSCoder *)aDecoder
Expand Down Expand Up @@ -89,27 +87,29 @@ - (void)initializer
[self.cellScrollView addSubview:_contentCellView];

// Add the cell scroll view to the cell
UIView *contentViewParent = self;
UIView *contentViewParent = _contentCellView;
UIView *clipViewParent = self.cellScrollView;
if (![NSStringFromClass([[self.subviews objectAtIndex:0] class]) isEqualToString:kTableViewCellContentView])
{
// iOS 7
contentViewParent = [self.subviews objectAtIndex:0];
clipViewParent = self;
}

NSArray *cellSubviews = [contentViewParent subviews];
[self insertSubview:self.cellScrollView atIndex:0];
[self.contentView insertSubview:self.cellScrollView atIndex:0];
for (UIView *subview in cellSubviews)
{
[_contentCellView addSubview:subview];
}

// Set scroll view to perpetually have same frame as self. Specifying relative to superview doesn't work, since the latter UITableViewCellScrollView has different behaviour.
[self addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0],
[self.contentView addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0],
]];

[self.cellScrollView addConstraints:@[
[NSLayoutConstraint constraintWithItem:_contentCellView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.cellScrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:_contentCellView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.cellScrollView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:_contentCellView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.cellScrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:_contentCellView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.cellScrollView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0],
]];

self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
Expand All @@ -127,13 +127,13 @@ - (void)initializer
// Such an approach is necessary in order for the utility views to sit on top to get taps, as well as allow the backgroundColor (and private UITableViewCellBackgroundView) to work properly.

self.leftUtilityClipView = [[UIView alloc] init];
self.leftUtilityClipConstraint = [NSLayoutConstraint constraintWithItem:self.leftUtilityClipView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
self.leftUtilityClipConstraint = [NSLayoutConstraint constraintWithItem:self.leftUtilityClipView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_cellScrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
self.leftUtilityButtonsView = [[SWUtilityButtonView alloc] initWithUtilityButtons:nil
parentCell:self
utilityButtonSelector:@selector(leftUtilityButtonHandler:)];

self.rightUtilityClipView = [[UIView alloc] initWithFrame:self.bounds];
self.rightUtilityClipConstraint = [NSLayoutConstraint constraintWithItem:self.rightUtilityClipView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
self.rightUtilityClipConstraint = [NSLayoutConstraint constraintWithItem:self.rightUtilityClipView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
self.rightUtilityButtonsView = [[SWUtilityButtonView alloc] initWithUtilityButtons:nil
parentCell:self
utilityButtonSelector:@selector(rightUtilityButtonHandler:)];
Expand All @@ -157,23 +157,22 @@ - (void)initializer
clipView.clipsToBounds = YES;

[clipViewParent addSubview:clipView];
[self addConstraints:@[
[self.contentView addConstraints:@[
// Pin the clipping view to the appropriate outer edges of the cell.
[NSLayoutConstraint constraintWithItem:clipView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:alignmentAttribute relatedBy:NSLayoutRelationEqual toItem:self attribute:alignmentAttribute multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:alignmentAttribute relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:alignmentAttribute multiplier:1.0 constant:0.0],
clipConstraint,
]];

[clipView addSubview:buttonView];
[self addConstraints:@[
[clipView addConstraints:@[
// Pin the button view to the appropriate outer edges of its clipping view.
[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:clipView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:clipView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:buttonView attribute:alignmentAttribute relatedBy:NSLayoutRelationEqual toItem:clipView attribute:alignmentAttribute multiplier:1.0 constant:0.0],

// Constrain the maximum button width so that at least a button's worth of contentView is left visible. (The button view will shrink accordingly.)
[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:-kUtilityButtonWidthDefault],
]];
}
}
Expand Down Expand Up @@ -601,7 +600,7 @@ - (void)updateCellState
}

// Update the clipping on the utility button views according to the current position.
CGRect frame = [self.contentView.superview convertRect:self.contentView.frame toView:self];
CGRect frame = [_cellScrollView convertRect:_contentCellView.frame toView:self];
frame.size.width = CGRectGetWidth(self.frame);

self.leftUtilityClipConstraint.constant = MAX(0, CGRectGetMinX(frame) - CGRectGetMinX(self.frame));
Expand Down