Skip to content
This repository was archived by the owner on Nov 29, 2022. It is now read-only.
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
15 changes: 15 additions & 0 deletions Source/UIScrollView+EmptyDataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")

/**
Expand Down Expand Up @@ -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


Expand Down
34 changes: 31 additions & 3 deletions Source/UIScrollView+EmptyDataSet.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down