diff --git a/Source/UIScrollView+EmptyDataSet.h b/Source/UIScrollView+EmptyDataSet.h index ddd45aa3..61435d97 100644 --- a/Source/UIScrollView+EmptyDataSet.h +++ b/Source/UIScrollView+EmptyDataSet.h @@ -12,9 +12,16 @@ NS_ASSUME_NONNULL_BEGIN +typedef NS_ENUM (NSUInteger, DZNEmptyDataSetVerticalAnchorLocation) { + DZNEmptyDataSetVerticalAnchorLocationCenter = 0, + DZNEmptyDataSetVerticalAnchorLocationTop, + DZNEmptyDataSetVerticalAnchorLocationBottom +}; + @protocol DZNEmptyDataSetSource; @protocol DZNEmptyDataSetDelegate; + #define DZNEmptyDataSetDeprecated(instead) DEPRECATED_MSG_ATTRIBUTE(" Use " # instead " instead") /** @@ -155,6 +162,14 @@ NS_ASSUME_NONNULL_BEGIN */ - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView; +/** + Asks the data source for a vertical anchor location. Default is DZNEmptyDataSetVerticalAnchorLocationCenter. + + @param scrollView A scrollview subclass object informing the data source. + @return A DZNEmptyDataSetVerticalAnchorLocation value indicating if the view should be pinned to the top, center, or bottom of its container. + */ +- (DZNEmptyDataSetVerticalAnchorLocation)verticalAnchorLocationForEmptyDataSet:(UIScrollView *)scrollView; + @end diff --git a/Source/UIScrollView+EmptyDataSet.m b/Source/UIScrollView+EmptyDataSet.m index 27d0a3a7..b124ba13 100644 --- a/Source/UIScrollView+EmptyDataSet.m +++ b/Source/UIScrollView+EmptyDataSet.m @@ -37,6 +37,7 @@ @interface DZNEmptyDataSetView : UIView @property (nonatomic, assign) CGFloat verticalOffset; @property (nonatomic, assign) CGFloat verticalSpace; +@property (nonatomic, assign) DZNEmptyDataSetVerticalAnchorLocation verticalAnchorLocation; @property (nonatomic, assign) BOOL fadeInOnDisplay; @@ -283,6 +284,15 @@ - (CGFloat)dzn_verticalSpace return 0.0; } +- (DZNEmptyDataSetVerticalAnchorLocation)dzn_verticalAnchorLocation +{ + if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(verticalAnchorLocationForEmptyDataSet:)]) { + return [self.emptyDataSetSource verticalAnchorLocationForEmptyDataSet:self]; + } + + return DZNEmptyDataSetVerticalAnchorLocationCenter; +} + #pragma mark - Delegate Getters & Events (Private) @@ -522,6 +532,9 @@ - (void)dzn_reloadEmptyDataSet // Configure offset view.verticalOffset = [self dzn_verticalOffset]; + // Configure anchor location + view.verticalAnchorLocation = [self dzn_verticalAnchorLocation]; + // Configure the empty dataset view view.backgroundColor = [self dzn_dataSetBackgroundColor]; view.hidden = NO; @@ -921,15 +934,30 @@ - (void)setupConstraints // First, configure the content view constaints // The content view must alway be centered to its superview NSLayoutConstraint *centerXConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterX]; - NSLayoutConstraint *centerYConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterY]; + NSLayoutConstraint *verticalAnchorConstraint = nil; + + switch (self.verticalAnchorLocation) { + case DZNEmptyDataSetVerticalAnchorLocationCenter: { + verticalAnchorConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterY]; + break; + } + case DZNEmptyDataSetVerticalAnchorLocationTop: { + verticalAnchorConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeTop]; + break; + } + case DZNEmptyDataSetVerticalAnchorLocationBottom: { + verticalAnchorConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeBottom]; + break; + } + } [self addConstraint:centerXConstraint]; - [self addConstraint:centerYConstraint]; + [self addConstraint:verticalAnchorConstraint]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]]; // When a custom offset is available, we adjust the vertical constraints' constants if (self.verticalOffset != 0 && self.constraints.count > 0) { - centerYConstraint.constant = self.verticalOffset; + verticalAnchorConstraint.constant = self.verticalOffset; } // If applicable, set the custom view's constraints