Skip to content

Commit

Permalink
Changed vertical anchor to be optionally configurable.
Browse files Browse the repository at this point in the history
Adds a new enum to control whether the empty view is pinned to the center, top, or bottom of its container. The vertical offset, if provided, will affect the offset of the provided anchor location (Top constant, if pinned to top. Center constant, if pinned to centerY, bottom constant, if pinned to bottom). This allows a bit more flexibility in how a client chooses to lay out the empty view to meet their unique requirements.
  • Loading branch information
colinhumber committed Apr 27, 2016
1 parent 8ee3ac4 commit fc2bc59
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Source/UIScrollView+EmptyDataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

#import <UIKit/UIKit.h>

typedef NS_ENUM (NSUInteger, DZNEmptyDataSetVerticalAnchorLocation) {
DZNEmptyDataSetVerticalAnchorLocationCenter = 0,
DZNEmptyDataSetVerticalAnchorLocationTop,
DZNEmptyDataSetVerticalAnchorLocationBottom
};


@protocol DZNEmptyDataSetSource;
@protocol DZNEmptyDataSetDelegate;

Expand Down Expand Up @@ -86,7 +93,7 @@
*
* @return image animation
*/
- (CAAnimation *) imageAnimationForEmptyDataSet:(UIScrollView *) scrollView;
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *) scrollView;

/**
Asks the data source for the title to be used for the specified button state.
Expand Down Expand Up @@ -153,6 +160,8 @@
*/
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;

- (DZNEmptyDataSetVerticalAnchorLocation)verticalAnchorLocationForEmptyDataSet:(UIScrollView *)scrollView;

@end


Expand Down
37 changes: 33 additions & 4 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 @@ -519,6 +529,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 @@ -923,17 +936,33 @@ - (void)prepareForReuse
- (void)setupConstraints
{
// First, configure the content view constaints
// The content view must alway be centered to its superview
// The content view must always 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

0 comments on commit fc2bc59

Please sign in to comment.