Skip to content

Commit

Permalink
Merge pull request #116 from brockboland/fixed_paging_autolayout
Browse files Browse the repository at this point in the history
Switched paging accessory to use autolayout
  • Loading branch information
brockboland authored Nov 30, 2016
2 parents 6624079 + 0224bf7 commit 1a915b3
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 82 deletions.
96 changes: 89 additions & 7 deletions Pod/Classes/Optional Data Sources/VOKDefaultPagingAccessory.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@interface VOKDefaultPagingAccessory ()

@property UIActivityIndicatorView *indicator;
@property (nonatomic) UIActivityIndicatorView *indicator;

@end

Expand All @@ -22,13 +22,11 @@ - (void)didMoveToSuperview
self.clipsToBounds = YES;
[self setBackgroundColor:[UIColor clearColor]];

UILabel *label = [[UILabel alloc] initWithFrame:(CGRect){CGPointZero, self.frame.size}];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[label setText:@"Pull To Load"];
[label setFont:[UIFont boldSystemFontOfSize:20]];
[label setTextAlignment:NSTextAlignmentCenter];
[self addSubview:label];
[label sizeToFit];
[label setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
label.translatesAutoresizingMaskIntoConstraints = NO;

UIActivityIndicatorViewStyle indicatorStyle;
#if TARGET_OS_IOS
Expand All @@ -40,9 +38,93 @@ - (void)didMoveToSuperview
self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:indicatorStyle];
[self.indicator setTintColor:[UIColor blackColor]];
[self.indicator setHidesWhenStopped:NO];
self.indicator.translatesAutoresizingMaskIntoConstraints = NO;

[self.indicator setCenter:CGPointMake(label.frame.origin.x - self.indicator.frame.size.width, self.frame.size.height/2)];
[self addSubview:self.indicator];
// Wrap the label and activity indicator in a parent view to be collectively centered
UIView *containerView = [[UIView alloc] init];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[containerView addSubview:self.indicator];
[containerView addSubview:label];
[self addSubview:containerView];

// Position the activity indicator and label
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.indicator
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0]];
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0]];
// Put a little space between the lable and activity indicator
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.indicator
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:20]];

// Vertically center the indicator and label inside the container
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:containerView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:self.indicator
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:containerView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];

// Make the container fill this view, and center it horizontally
[self addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];

// Don't go too wide
[self addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:self
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0]];
}
}

Expand Down
Loading

0 comments on commit 1a915b3

Please sign in to comment.