Skip to content

Added UIPage Control #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits 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
4 changes: 2 additions & 2 deletions Classes/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (void)viewDidLoad {
[mostViewed release];

// Don't show scroll indicator
self.tabBar.showsHorizontalScrollIndicator = NO;
self.tabBar.theScrollView.showsHorizontalScrollIndicator = NO;
self.tabBar.infiniTabBarDelegate = self;
self.tabBar.bounces = NO;

Expand Down Expand Up @@ -169,7 +169,7 @@ - (void)bounces:(UISwitch *)sender {
}

- (void)showsScrollIndicator:(UISwitch *)sender {
self.tabBar.showsHorizontalScrollIndicator = sender.on;
[self.tabBar setShowsHorizontalScroller:sender.on];
}

- (void)setNewItems {
Expand Down
11 changes: 10 additions & 1 deletion InfiniTabBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,33 @@

@protocol InfiniTabBarDelegate;

@interface InfiniTabBar : UIScrollView <UIScrollViewDelegate, UITabBarDelegate> {
@interface InfiniTabBar : UIView <UIScrollViewDelegate, UITabBarDelegate> {
id <InfiniTabBarDelegate> infiniTabBarDelegate;
NSMutableArray *tabBars;
UITabBar *aTabBar;
UITabBar *bTabBar;
UIScrollView * theScrollView;
UIPageControl * pageControl;
}

@property (nonatomic, assign) id infiniTabBarDelegate;
@property (nonatomic, retain) NSMutableArray *tabBars;
@property (nonatomic, retain) UITabBar *aTabBar;
@property (nonatomic, retain) UITabBar *bTabBar;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIScrollView *theScrollView;

- (id)initWithItems:(NSArray *)items;
- (void)setBounces:(BOOL)bounces;
- (void)setShowsHorizontalScroller:(BOOL)show;
- (void)setShowsPageControl:(BOOL)showsPageControl;

// Don't set more items than initially
- (void)setItems:(NSArray *)items animated:(BOOL)animated;
- (int)currentTabBarTag;
- (int)selectedItemTag;
-(void)selectFirstItem:(BOOL)animated;

- (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated;
- (BOOL)selectItemWithTag:(int)tag;

Expand Down
128 changes: 104 additions & 24 deletions InfiniTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,38 @@ @implementation InfiniTabBar
@synthesize tabBars;
@synthesize aTabBar;
@synthesize bTabBar;
@synthesize pageControl;
@synthesize theScrollView;

- (id)initWithItems:(NSArray *)items {
self = [super initWithFrame:CGRectMake(0.0, 411.0, 320.0, 49.0)];
self = [super initWithFrame:CGRectMake(0.0, 300.0, 320.0, 110.0)];
// TODO:
//self = [super initWithFrame:CGRectMake(self.superview.frame.origin.x + self.superview.frame.size.width - 320.0, self.superview.frame.origin.y + self.superview.frame.size.height - 49.0, 320.0, 49.0)];
//self = [super initWithFrame:CGRectMake(self.superview.frame.origin.x + self.superview.frame.size.width - 320.0, self.superview.frame.origin.y + self.superview.frame.size.height - 59.0, 320.0, 59.0)];
// Doesn't work. self is nil at this point.

if (self) {
self.pagingEnabled = YES;
self.delegate = self;
[self setBackgroundColor:[UIColor clearColor]];
theScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0, 320.0, 110.0)];
theScrollView.pagingEnabled = YES;
theScrollView.delegate = self;

self.tabBars = [[[NSMutableArray alloc] init] autorelease];

float x = 0.0;

for (double d = 0; d < ceil(items.count / 5.0); d ++) {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, 320.0, 49.0)];
for (double d = 0; d < ceil(items.count / 4.0); d ++) {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, 320.0, 80.0)];
tabBar.delegate = self;

int len = 0;

for (int i = d * 5; i < d * 5 + 5; i ++)
for (int i = d * 4; i < d * 4 + 4; i ++)
if (i < items.count)
len ++;

tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 5, len)]];
tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 4, len)]];

[self addSubview:tabBar];
[self.theScrollView addSubview:tabBar];

[self.tabBars addObject:tabBar];

Expand All @@ -47,7 +51,9 @@ - (id)initWithItems:(NSArray *)items {
x += 320.0;
}

self.contentSize = CGSizeMake(x, 49.0);
self.theScrollView.contentSize = CGSizeMake(x, 110.0);
[self addSubview:self.theScrollView];
[self setShowsPageControl:YES];
}

return self;
Expand All @@ -59,39 +65,99 @@ - (void)setBounces:(BOOL)bounces {

if (count > 0) {
if (self.aTabBar == nil)
self.aTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(-320.0, 0.0, 320.0, 49.0)]autorelease];
self.aTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(-320.0, 0.0, 320.0, 110.0)]autorelease];

[self addSubview:self.aTabBar];
[[self theScrollView] addSubview:self.aTabBar];

if (self.bTabBar == nil)
self.bTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(count * 320.0, 0.0, 320.0, 49.0)] autorelease];
self.bTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(count * 320.0, 0.0, 320.0, 110.0)] autorelease];

[self addSubview:self.bTabBar];
[[self theScrollView] addSubview:self.bTabBar];
}
} else {
[self.aTabBar removeFromSuperview];
[self.bTabBar removeFromSuperview];
}

[super setBounces:bounces];
[self.theScrollView setBounces:bounces];
}

- (void)setShowsPageControl:(BOOL)showsPageControl {
if(showsPageControl) {
if(!self.pageControl) {
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-30, self.frame.size.width, 20)];
[self.pageControl setEnabled:FALSE];
[self addSubview:self.pageControl];
}
int count = self.tabBars.count;
[self.pageControl setNumberOfPages:count];

} else if(pageControl) {
[self.pageControl removeFromSuperview];
self.pageControl = nil;

}
}

-(void)setShowsHorizontalScroller:(BOOL)show {
[self.theScrollView setShowsHorizontalScrollIndicator:show];
}

- (void)setItems:(NSArray *)items animated:(BOOL)animated {
int itemCount = 0;

if(ceil(items.count / 4.0) < self.tabBars.count) {
for(int i = self.tabBars.count-1; i+1 > ceil(items.count / 4.0); i--) {
[self.tabBars removeObjectAtIndex:i];
}
}

for (UITabBar *tabBar in self.tabBars) {
int len = 0;

for (int i = [self.tabBars indexOfObject:tabBar] * 5; i < [self.tabBars indexOfObject:tabBar] * 5 + 5; i ++)
for (int i = [self.tabBars indexOfObject:tabBar] * 4; i < [self.tabBars indexOfObject:tabBar] * 4 + 4; i ++)
if (i < items.count)
len ++;

[tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.tabBars indexOfObject:tabBar] * 5, len)]] animated:animated];
}
[tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.tabBars indexOfObject:tabBar] * 4, len)]] animated:animated];
itemCount = itemCount + tabBar.items.count;

}



float x = self.tabBars.count * 320;

if(itemCount != items.count) {
for (double d = self.tabBars.count; d < ceil(items.count / 4.0); d ++) {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, 320.0, 80.0)];
tabBar.delegate = self;

int len = 0;

for (int i = d * 4; i < d * 4 + 4; i ++)
if (i < items.count)
len ++;

tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 4, len)]];

[self.theScrollView addSubview:tabBar];

[self.tabBars addObject:tabBar];

[tabBar release];

x += 320.0;
}
}

self.contentSize = CGSizeMake(ceil(items.count / 5.0) * 320.0, 49.0);
self.theScrollView.contentSize = CGSizeMake(self.tabBars.count * 320.0, 110.0);
if(self.pageControl != nil) {
[self.pageControl setNumberOfPages:self.tabBars.count];
}

}

- (int)currentTabBarTag {
return self.contentOffset.x / 320.0;
return self.theScrollView.contentOffset.x / 320.0;
}

- (int)selectedItemTag {
Expand All @@ -108,10 +174,10 @@ - (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated {
if ([self.tabBars indexOfObject:tabBar] == tag) {
UITabBar *tabBar = [self.tabBars objectAtIndex:tag];

[self scrollRectToVisible:tabBar.frame animated:animated];
[self.theScrollView scrollRectToVisible:tabBar.frame animated:animated];

if (animated == NO)
[self scrollViewDidEndDecelerating:self];
[self scrollViewDidEndDecelerating:self.theScrollView];

return YES;
}
Expand All @@ -133,7 +199,21 @@ - (BOOL)selectItemWithTag:(int)tag {
return NO;
}


-(void)selectFirstItem:(BOOL)animated {
[self scrollToTabBarWithTag:0 animated:animated];
if([self.tabBars count] > 0) {
UITabBarItem *firstItem = [[[self.tabBars objectAtIndex:0] items] objectAtIndex:0];
[self selectItemWithTag:firstItem.tag];
}


}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if(pageControl) {
[pageControl setCurrentPage:scrollView.contentOffset.x / 320.0];
}
[infiniTabBarDelegate infiniTabBar:self didScrollToTabBarWithTag:scrollView.contentOffset.x / 320.0];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "InfiniTabBar.app"
BlueprintName = "InfiniTabBar"
ReferencedContainer = "container:InfiniTabBar.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "InfiniTabBar.app"
BlueprintName = "InfiniTabBar"
ReferencedContainer = "container:InfiniTabBar.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "InfiniTabBar.app"
BlueprintName = "InfiniTabBar"
ReferencedContainer = "container:InfiniTabBar.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "InfiniTabBar.app"
BlueprintName = "InfiniTabBar"
ReferencedContainer = "container:InfiniTabBar.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>InfiniTabBar.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>1D6058900D05DD3D006BFB54</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>