Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Allows the Pager to be opened at any index #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion ICViewPager/Controller/HostViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ - (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewCont
- (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value {

switch (option) {
case ViewPagerOptionStartFromSecondTab:
case ViewPagerOptionInitialIndex:
return 0.0;
case ViewPagerOptionCenterCurrentTab:
return 1.0;
Expand Down
4 changes: 2 additions & 2 deletions ICViewPager/ICViewPager/ViewPagerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* ViewPagerOptionTabOffset: Tab bar's offset from left, defaults to 56.0
* ViewPagerOptionTabWidth: Any tab item's width, defaults to 128.0
* ViewPagerOptionTabLocation: 1.0: Top, 0.0: Bottom, Defaults to Top
* ViewPagerOptionStartFromSecondTab: 1.0: YES, 0.0: NO, defines if view should appear with the 1st or 2nd tab. Defaults to NO
* ViewPagerOptionInitialIndex: Defines which tab the pager should open at. Defaults to 0 (i.e. the first tab). Should be an integer.
* ViewPagerOptionCenterCurrentTab: 1.0: YES, 0.0: NO, defines if tabs should be centered, with the given tabWidth. Defaults to NO
* ViewPagerOptionFixFormerTabsPositions: 1.0: YES, 0.0: NO, defines if the active tab should be placed margined by the offset amount to the left. Effects only the former tabs. If set 1.0 (YES), first tab will be placed at the same position with the second one, leaving space before itself. Defaults to NO
* ViewPagerOptionFixLatterTabsPositions: 1.0: YES, 0.0: NO, like ViewPagerOptionFixFormerTabsPositions, but effects the latter tabs, making them leave space after themselves. Defaults to NO
Expand All @@ -25,7 +25,7 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) {
ViewPagerOptionTabOffset,
ViewPagerOptionTabWidth,
ViewPagerOptionTabLocation,
ViewPagerOptionStartFromSecondTab,
ViewPagerOptionInitialIndex,
ViewPagerOptionCenterCurrentTab,
ViewPagerOptionFixFormerTabsPositions,
ViewPagerOptionFixLatterTabsPositions
Expand Down
36 changes: 19 additions & 17 deletions ICViewPager/ICViewPager/ViewPagerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define kTabOffset 56.0
#define kTabWidth 128.0
#define kTabLocation 1.0
#define kStartFromSecondTab 0.0
#define kInitialIndex 0.0
#define kCenterCurrentTab 0.0
#define kFixFormerTabsPositions 0.0
#define kFixLatterTabsPositions 0.0
Expand Down Expand Up @@ -131,7 +131,7 @@ @interface ViewPagerController () <UIPageViewControllerDataSource, UIPageViewCon
@property (nonatomic) NSNumber *tabOffset;
@property (nonatomic) NSNumber *tabWidth;
@property (nonatomic) NSNumber *tabLocation;
@property (nonatomic) NSNumber *startFromSecondTab;
@property (nonatomic) NSNumber *initialIndex;
@property (nonatomic) NSNumber *centerCurrentTab;
@property (nonatomic) NSNumber *fixFormerTabsPositions;
@property (nonatomic) NSNumber *fixLatterTabsPositions;
Expand All @@ -156,7 +156,7 @@ @implementation ViewPagerController
@synthesize tabOffset = _tabOffset;
@synthesize tabWidth = _tabWidth;
@synthesize tabLocation = _tabLocation;
@synthesize startFromSecondTab = _startFromSecondTab;
@synthesize initialIndex = _initialIndex;
@synthesize centerCurrentTab = _centerCurrentTab;
@synthesize fixFormerTabsPositions = _fixFormerTabsPositions;
@synthesize fixLatterTabsPositions = _fixLatterTabsPositions;
Expand Down Expand Up @@ -285,12 +285,14 @@ - (void)setTabLocation:(NSNumber *)tabLocation {

_tabLocation = tabLocation;
}
- (void)setStartFromSecondTab:(NSNumber *)startFromSecondTab {
- (void)setInitialIndex:(NSNumber *)initialIndex {

if ([startFromSecondTab floatValue] != 1.0 && [startFromSecondTab floatValue] != 0.0)
startFromSecondTab = [startFromSecondTab boolValue] ? [NSNumber numberWithBool:YES] : [NSNumber numberWithBool:NO];
if([initialIndex floatValue] < 0.0)
initialIndex = [NSNumber numberWithFloat:0.0];
else if ([initialIndex floatValue] > self.tabCount)
initialIndex = [NSNumber numberWithFloat:0.0];

_startFromSecondTab = startFromSecondTab;
_initialIndex = initialIndex;
}
- (void)setCenterCurrentTab:(NSNumber *)centerCurrentTab {

Expand Down Expand Up @@ -475,15 +477,15 @@ - (NSNumber *)tabLocation {
}
return _tabLocation;
}
- (NSNumber *)startFromSecondTab {
- (NSNumber *)initialIndex {

if (!_startFromSecondTab) {
CGFloat value = kStartFromSecondTab;
if (!_initialIndex) {
CGFloat value = kInitialIndex;
if ([self.delegate respondsToSelector:@selector(viewPager:valueForOption:withDefault:)])
value = [self.delegate viewPager:self valueForOption:ViewPagerOptionStartFromSecondTab withDefault:value];
self.startFromSecondTab = [NSNumber numberWithFloat:value];
value = [self.delegate viewPager:self valueForOption:ViewPagerOptionInitialIndex withDefault:value];
self.initialIndex = [NSNumber numberWithFloat:value];
}
return _startFromSecondTab;
return _initialIndex;
}
- (NSNumber *)centerCurrentTab {

Expand Down Expand Up @@ -560,7 +562,7 @@ - (void)reloadData {
_tabOffset = nil;
_tabWidth = nil;
_tabLocation = nil;
_startFromSecondTab = nil;
_initialIndex = nil;
_centerCurrentTab = nil;
_fixFormerTabsPositions = nil;
_fixLatterTabsPositions = nil;
Expand Down Expand Up @@ -731,8 +733,8 @@ - (CGFloat)valueForOption:(ViewPagerOption)option {
return [[self tabWidth] floatValue];
case ViewPagerOptionTabLocation:
return [[self tabLocation] floatValue];
case ViewPagerOptionStartFromSecondTab:
return [[self startFromSecondTab] floatValue];
case ViewPagerOptionInitialIndex:
return [[self initialIndex] floatValue];
case ViewPagerOptionCenterCurrentTab:
return [[self centerCurrentTab] floatValue];
default:
Expand Down Expand Up @@ -875,7 +877,7 @@ - (void)defaultSetup {
}

// Select starting tab
NSUInteger index = [self.startFromSecondTab boolValue] ? 1 : 0;
NSUInteger index = [self.initialIndex integerValue];
[self selectTabAtIndex:index didSwipe:NO];

// Set setup done
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Every option has a default value. So
* `ViewPagerOptionTabOffset`: Tab bar's offset from left, defaults to 56.0
* `ViewPagerOptionTabWidth`: Any tab item's width, defaults to 128.0
* `ViewPagerOptionTabLocation`: 1.0: Top, 0.0: Bottom, Defaults to Top
* `ViewPagerOptionStartFromSecondTab`: 1.0: `YES`, 0.0: `NO`, defines if view should appear with the 1st or 2nd tab. Defaults to `NO`
* `ViewPagerOptionInitialIndex`: Defines which tab the pager should open at. Defaults to `0.0` (i.e. the first tab). Should be an integer.
* `ViewPagerOptionCenterCurrentTab`: 1.0: `YES`, 0.0: `NO`, defines if tabs should be centered, with the given tabWidth. Defaults to `NO`
* `ViewPagerOptionFixFormerTabsPositions`: 1.0: `YES`, 0.0: `NO`, defines if the active tab should be placed margined by the offset amount to the left. Effects only the former tabs. If set 1.0 (`YES`), first tab will be placed at the same position with the second one, leaving space before itself. Defaults to `NO`
* `ViewPagerOptionFixLatterTabsPositions`: 1.0: `YES`, 0.0: `NO`, like `ViewPagerOptionFixFormerTabsPositions`, but effects the latter tabs, making them leave space after themselves. Defaults to `NO`
Expand Down