diff --git a/README.md b/README.md index 6729b5e..da1cbaf 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ XLPagerTabStrip By [XMARTLABS](http://xmartlabs.com). +[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/xmartlabs/XLPagerTabStrip/blob/master/LICENSE) +[![version](https://img.shields.io/badge/pod-2.0.0-blue.svg)](https://github.com/xmartlabs/XLPagerTabStrip/releases) + Android [PagerTabStrip](http://developer.android.com/reference/android/support/v4/view/PagerTabStrip.html) for iOS! **XLPagerTabStrip** is a *Container View Controller* that allows us to switch easily among a collection of view controllers. Pan gesture can be used to move on to next or previous view controller. It shows a interactive indicator of the current, previous, next child view controllers. @@ -40,7 +43,9 @@ FAQ ```objc -(void)moveToViewControllerAtIndex:(NSUInteger)index; +-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated; -(void)moveToViewController:(UIViewController *)viewController; +-(void)moveToViewController:(UIViewController *)viewController animated:(BOOL)animated; ``` #####How to change the selected tab (XLButtonBarViewCell) look and feel based on the selected state @@ -59,7 +64,7 @@ Installation The easiest way to use `XLPagerTabStrip` in your app is via [CocoaPods](http://cocoapods.org/ "CocoaPods"). 1. Add the following line in the project's Podfile file: -`pod 'XLPagerTabStrip', '~> 1.0'`. +`pod 'XLPagerTabStrip', '~> 2.0'`. 2. Run the command `pod install` from the Podfile folder directory. @@ -85,7 +90,7 @@ Requirements Release Notes -------------- -Version 2.0.0 (master) +Version 2.0.0 * Added ability to change look and feel of selected tab. * `changeCurrentIndexProgressiveBlock` added to `XLButtonBarPagerTabStripViewController`. @@ -96,6 +101,7 @@ Version 2.0.0 (master) * Bug Fix Issue #54: Twitter PagerTabStrip wasn't loading the navigation title correctly. * Bug Fix Issue #32: Demo for Nav Button Bar Example fix. * Bug Fix Issue #32: Twitter Pager white dots that mark which tab is currently selected is non selectable now. +* Bug Fix Issue #22: moveToViewControllerAtIndex: in viewDidLoad or viewWillAppear is not reflected in buttonBarView. Version 1.1.1 diff --git a/XLPagerTabStrip/XL/Controllers/XLButtonBarPagerTabStripViewController.m b/XLPagerTabStrip/XL/Controllers/XLButtonBarPagerTabStripViewController.m index 94c8fd0..ac0efc4 100644 --- a/XLPagerTabStrip/XL/Controllers/XLButtonBarPagerTabStripViewController.m +++ b/XLPagerTabStrip/XL/Controllers/XLButtonBarPagerTabStripViewController.m @@ -80,9 +80,8 @@ -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - UICollectionViewLayoutAttributes *attributes = [self.buttonBarView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]]; - CGRect cellRect = attributes.frame; - [self.buttonBarView.selectedBar setFrame:CGRectMake(cellRect.origin.x, self.buttonBarView.frame.size.height - self.buttonBarView.selectedBarHeight, cellRect.size.width, self.buttonBarView.selectedBarHeight)]; + [self.buttonBarView layoutIfNeeded]; + [self.buttonBarView moveToIndex:self.currentIndex animated:NO swipeDirection:XLPagerTabStripDirectionNone pagerScroll:(self.isProgressiveIndicator ? XLPagerScrollYES :XLPagerScrollOnlyIfOutOfScreen)]; } -(void)reloadPagerTabStripView @@ -90,7 +89,7 @@ [super reloadPagerTabStripView]; if ([self isViewLoaded]){ [self.buttonBarView reloadData]; - [self.buttonBarView moveToIndex:self.currentIndex animated:NO swipeDirection:XLPagerTabStripDirectionNone]; + [self.buttonBarView moveToIndex:self.currentIndex animated:NO swipeDirection:XLPagerTabStripDirectionNone pagerScroll:XLPagerScrollYES]; } } @@ -121,7 +120,7 @@ if (toIndex < fromIndex){ direction = XLPagerTabStripDirectionRight; } - [self.buttonBarView moveToIndex:toIndex animated:YES swipeDirection:direction]; + [self.buttonBarView moveToIndex:toIndex animated:YES swipeDirection:direction pagerScroll:XLPagerScrollYES]; if (self.changeCurrentIndexBlock) { XLButtonBarViewCell *oldCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex != fromIndex ? fromIndex : toIndex inSection:0]]; XLButtonBarViewCell *newCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]]; @@ -139,7 +138,7 @@ if (self.shouldUpdateButtonBarView){ [self.buttonBarView moveFromIndex:fromIndex toIndex:toIndex - withProgressPercentage:progressPercentage]; + withProgressPercentage:progressPercentage pagerScroll:XLPagerScrollYES]; if (self.changeCurrentIndexProgressiveBlock) { XLButtonBarViewCell *oldCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex != fromIndex ? fromIndex : toIndex inSection:0]]; @@ -172,7 +171,7 @@ if (indexPath.item == self.currentIndex) return; - [self.buttonBarView moveToIndex:indexPath.item animated:YES swipeDirection:XLPagerTabStripDirectionNone]; + [self.buttonBarView moveToIndex:indexPath.item animated:YES swipeDirection:XLPagerTabStripDirectionNone pagerScroll:XLPagerScrollYES]; self.shouldUpdateButtonBarView = NO; XLButtonBarViewCell *oldCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]]; diff --git a/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.h b/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.h index b76a1ef..16af595 100644 --- a/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.h +++ b/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.h @@ -94,9 +94,9 @@ typedef NS_ENUM(NSUInteger, XLPagerTabStripDirection) { @property BOOL isElasticIndicatorLimit; -(void)moveToViewControllerAtIndex:(NSUInteger)index; --(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(bool)animated; +-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated; -(void)moveToViewController:(UIViewController *)viewController; --(void)moveToViewController:(UIViewController *)viewController animated:(bool)animated; +-(void)moveToViewController:(UIViewController *)viewController animated:(BOOL)animated; -(void)reloadPagerTabStripView; @end diff --git a/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.m b/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.m index 0a1016d..f23b046 100644 --- a/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.m +++ b/XLPagerTabStrip/XL/Controllers/XLPagerTabStripViewController.m @@ -133,9 +133,9 @@ } --(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(bool)animated +-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated { - if (![self isViewLoaded]){ + if (!self.isViewLoaded || !self.view.window){ self.currentIndex = index; } else{ @@ -170,7 +170,7 @@ [self moveToViewControllerAtIndex:[self.pagerTabStripChildViewControllers indexOfObject:viewController]]; } --(void)moveToViewController:(UIViewController *)viewController animated:(bool)animated { +-(void)moveToViewController:(UIViewController *)viewController animated:(BOOL)animated { [self moveToViewControllerAtIndex:[self.pagerTabStripChildViewControllers indexOfObject:viewController] animated:animated]; } diff --git a/XLPagerTabStrip/XL/Views/XLButtonBarView.h b/XLPagerTabStrip/XL/Views/XLButtonBarView.h index f59c75e..96cd804 100644 --- a/XLPagerTabStrip/XL/Views/XLButtonBarView.h +++ b/XLPagerTabStrip/XL/Views/XLButtonBarView.h @@ -27,6 +27,11 @@ #import "XLPagerTabStripViewController.h" +typedef NS_ENUM(NSUInteger, XLPagerScroll) { + XLPagerScrollNO, + XLPagerScrollYES, + XLPagerScrollOnlyIfOutOfScreen +}; @interface XLButtonBarView : UICollectionView @@ -35,9 +40,9 @@ @property UIFont * labelFont; @property NSUInteger leftRightMargin; --(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection; +-(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection pagerScroll:(XLPagerScroll)pagerScroll; --(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage; +-(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage pagerScroll:(XLPagerScroll)pagerScroll; diff --git a/XLPagerTabStrip/XL/Views/XLButtonBarView.m b/XLPagerTabStrip/XL/Views/XLButtonBarView.m index 8ca3847..cd15e88 100644 --- a/XLPagerTabStrip/XL/Views/XLButtonBarView.m +++ b/XLPagerTabStrip/XL/Views/XLButtonBarView.m @@ -74,13 +74,13 @@ } --(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection +-(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection pagerScroll:(XLPagerScroll)pagerScroll { self.selectedOptionIndex = index; - [self updateSelectedBarPositionWithAnimation:animated swipeDirection:swipeDirection]; + [self updateSelectedBarPositionWithAnimation:animated swipeDirection:swipeDirection pagerScroll:pagerScroll]; } --(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage +-(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage pagerScroll:(XLPagerScroll)pagerScroll { self.selectedOptionIndex = (progressPercentage > 0.5 ) ? toIndex : fromIndex; @@ -110,21 +110,22 @@ if (self.contentSize.width > self.frame.size.width){ xValue = MIN(self.contentSize.width - self.frame.size.width, targetFrame.origin.x - offset <= 0 ? 0 : targetFrame.origin.x - offset); } - //NSLog(@"X value: %@", @(xValue)); - [self setContentOffset:CGPointMake(xValue, 0) animated:NO]; + [self setContentOffset:CGPointMake(xValue, 0) animated:(ABS(self.contentOffset.x - xValue) > 30)]; self.selectedBar.frame = CGRectMake(targetFrame.origin.x, self.selectedBar.frame.origin.y, targetFrame.size.width, self.selectedBar.frame.size.height); } --(void)updateSelectedBarPositionWithAnimation:(BOOL)animation swipeDirection:(XLPagerTabStripDirection)swipeDirection +-(void)updateSelectedBarPositionWithAnimation:(BOOL)animation swipeDirection:(XLPagerTabStripDirection __unused)swipeDirection pagerScroll:(XLPagerScroll)pagerScroll { CGRect frame = self.selectedBar.frame; - UICollectionViewCell * cell = [self.dataSource collectionView:self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedOptionIndex inSection:0]]; - [self updateContentOffset]; + UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedOptionIndex inSection:0]]; + CGRect cellFrame = attributes.frame; - frame.size.width = cell.frame.size.width; - frame.origin.x = cell.frame.origin.x; + [self updateContentOffsetAnimated:animation pagerScroll:pagerScroll toFrame:cellFrame]; + + frame.size.width = cellFrame.size.width; + frame.origin.x = cellFrame.origin.x; if (animation){ [UIView animateWithDuration:0.3 animations:^{ [self.selectedBar setFrame:frame]; @@ -139,19 +140,22 @@ #pragma mark - Helpers --(void)updateContentOffset +-(void)updateContentOffsetAnimated:(BOOL)animated pagerScroll:(XLPagerScroll)pageScroller toFrame:(CGRect)frame { - UICollectionViewCell * cell = [self.dataSource collectionView:self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedOptionIndex inSection:0]]; - if (cell){ - NSUInteger offset = 35; + if (pageScroller != XLPagerScrollNO){ + CGFloat leftInset = ((UICollectionViewFlowLayout *)self.collectionViewLayout).sectionInset.left; + if (pageScroller == XLPagerScrollOnlyIfOutOfScreen){ + if (frame.origin.x >= self.contentOffset.x && frame.origin.x < (self.contentOffset.x + self.frame.size.width - leftInset)){ + return; + } + } + NSUInteger offset = 30; float xValue = MIN( MAX(0, - self.contentSize.width - self.frame.size.width), // dont scroll if we are at the end of scroll view, if content is smaller than container width we scroll 0 - MAX(((UICollectionViewFlowLayout *)self.collectionViewLayout).sectionInset.left - cell.frame.origin.x, - cell.frame.origin.x - ((UICollectionViewFlowLayout *)self.collectionViewLayout).sectionInset.left - offset) - + self.collectionViewLayout.collectionViewContentSize.width - self.frame.size.width), // dont scroll if we are at the end of scroll view, if content is smaller than container width we scroll 0 + MAX(leftInset - frame.origin.x, frame.origin.x - leftInset - offset) ); - [self setContentOffset:CGPointMake(xValue, 0) animated:YES]; + [self setContentOffset:CGPointMake(xValue, 0) animated:animated]; } }