Skip to content

Commit

Permalink
Merge pull request #3 from mirego/fix/pager_strip_with_titles
Browse files Browse the repository at this point in the history
Change pager strip viewControllers for titles
  • Loading branch information
simondec committed Apr 6, 2016
2 parents eda5556 + 482fb7d commit 0767975
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions Pod/Classes/MRGPagerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (void)viewDidLoad {
if (self.pagerStrip) {
[self.view addSubview:self.pagerStrip];
self.pagerStrip.delegate = self;
self.pagerStrip.viewControllers = self.viewControllers;
self.pagerStrip.pageTitles = [self getPageTitles];
[self.view addSubview:self.pagerStrip];
}

Expand Down Expand Up @@ -299,8 +299,8 @@ - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
if (_viewControllers != viewControllers) {
NSArray *oldViewControllers = self.viewControllers;
_viewControllers = [viewControllers copy];
[self.pagerStrip setViewControllers:self.viewControllers animated:animated];

[self.pagerStrip setPageTitles:[self getPageTitles] animated:animated];
[self updateViewControllersWithOldViewControllers:oldViewControllers newViewControllers:self.viewControllers animated:animated];

if ((self.pagerScrollView != nil)) {
Expand Down Expand Up @@ -339,14 +339,23 @@ - (void)setPadding:(UIEdgeInsets)padding {
}
}

- (NSArray *)getPageTitles {
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:self.viewControllers.count];
for (UIViewController *viewController in self.viewControllers) {
[titles addObject:viewController.title];
}
return titles;
}

#pragma mark - MRGPagerStripDelegate

- (void)pagerStripSizeChanged:(id<MRGPagerStrip>)pagerStrip {
[self.view setNeedsLayout];
}

- (void)pagerStrip:(id<MRGPagerStrip>)pagerStrip didSelectViewController:(UIViewController *)viewController {
[self setCurrentViewController:viewController animated:YES];
- (void)pagerStrip:(id <MRGPagerStrip>)pagerStrip didSelectPageAtIndex:(NSInteger)pageIndex
{
[self setCurrentViewController:self.viewControllers[pageIndex] animated:YES];
}

@end
6 changes: 3 additions & 3 deletions Pod/Classes/MRGPagerStrip.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

@property (nonatomic, weak) id<MRGPagerStripDelegate> delegate;

@property (nonatomic, copy) NSArray *viewControllers;
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;
@property (nonatomic, copy) NSArray *pageTitles;
- (void)setPageTitles:(NSArray *)pageTitles animated:(BOOL)animated;

@property (nonatomic) CGFloat currentIndex;
- (void)setCurrentIndex:(CGFloat)currentIndex animated:(BOOL)animated;
Expand All @@ -45,6 +45,6 @@
@protocol MRGPagerStripDelegate <NSObject>

- (void)pagerStripSizeChanged:(id<MRGPagerStrip>)pagerStrip;
- (void)pagerStrip:(id<MRGPagerStrip>)pagerStrip didSelectViewController:(UIViewController *)viewController;
- (void)pagerStrip:(id <MRGPagerStrip>)pagerStrip didSelectPageAtIndex:(NSUInteger)pageIndex;

@end
11 changes: 5 additions & 6 deletions Pod/Classes/MRGPagerTabStrip.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ @interface MRGPagerTabStrip () <UIScrollViewDelegate>

@implementation MRGPagerTabStrip

- (instancetype)initWithFrame:(CGRect)frame
{
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSParameterAssert(self.scrollView.delegate == nil);
Expand Down Expand Up @@ -110,8 +109,8 @@ - (void)layoutSubviews {

#pragma mark - get/set

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
[super setViewControllers:viewControllers animated:animated];
- (void)setPageTitles:(NSArray *)pageTitles animated:(BOOL)animated {
[super setPageTitles:pageTitles animated:animated];

[self.buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger index, BOOL *stop) {
[button setTag:index];
Expand All @@ -133,8 +132,8 @@ - (UIImage *)backgroundImageWithColor:(UIColor *)color {

- (void)buttonTapped:(UIButton *)button {
NSUInteger index = button.tag;
if (index < self.viewControllers.count) {
[self.delegate pagerStrip:self didSelectViewController:[self.viewControllers objectAtIndex:index]];
if (index < self.pageTitles.count) {
[self.delegate pagerStrip:self didSelectPageAtIndex:index];
}
}

Expand Down
23 changes: 9 additions & 14 deletions Pod/Classes/MRGPagerTitleStrip.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ @interface MRGPagerTitleStrip ()

@implementation MRGPagerTitleStrip

@synthesize viewControllers = _viewControllers;
@synthesize pageTitles = _pageTitles;
@synthesize currentIndex = _currentIndex;
@synthesize delegate = _delegate;

- (instancetype)initWithFrame:(CGRect)frame
{
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
Expand Down Expand Up @@ -182,9 +181,9 @@ - (void)updateButtonsAnimated:(BOOL)animated {
[self.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.buttons removeAllObjects];

for (UIViewController *viewController in self.viewControllers) {
for (NSString *title in self.pageTitles) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:viewController.title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[self.scrollView addSubview:button];
[self.buttons addObject:button];
}
Expand Down Expand Up @@ -238,17 +237,17 @@ - (void)updateButtonIndex:(CGFloat)index {

#pragma mark - get/set

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
if (_viewControllers != viewControllers) {
_viewControllers = [viewControllers copy];
- (void)setPageTitles:(NSArray *)pageTitles animated:(BOOL)animated {
if (_pageTitles != pageTitles) {
_pageTitles = [pageTitles copy];

[self updateButtonsAnimated:animated];
[self setNeedsUpdateView];
}
}

- (void)setViewControllers:(NSArray *)viewControllers {
[self setViewControllers:viewControllers animated:NO];
- (void)setPageTitles:(NSArray *)pageTitles {
[self setPageTitles:pageTitles animated:NO];
}

- (void)setCurrentIndex:(CGFloat)currentIndex animated:(BOOL)animated {
Expand All @@ -259,10 +258,6 @@ - (void)setCurrentIndex:(CGFloat)currentIndex animated:(BOOL)animated {
}
}

- (void)setCurrentViewControllerIndex:(CGFloat)index {
[self setCurrentIndex:index animated:NO];
}

- (void)setTitleTextAlignment:(NSTextAlignment)titleTextAlignment {
if (_titleTextAlignment != titleTextAlignment) {
_titleTextAlignment = titleTextAlignment;
Expand Down

0 comments on commit 0767975

Please sign in to comment.