-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomTabBarViewController.m
160 lines (132 loc) · 5 KB
/
CustomTabBarViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// CustomTabBarViewController.m
// CustomTabBar
//
// Created by Arpit on 7/11/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "CustomTabBarViewController.h"
#import "TabSectionView.h"
@implementation CustomTabBarViewController
@synthesize _nibNameString, bgImageView,buttonContainerView;
#pragma mark -
#pragma mark VIEW LIFE CYCLE
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self._nibNameString = nibNameOrNil;
if (self) {
// Custom initialization.
//[self performSelector:@selector(setTabBarDesign)];
}
return self;
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark -
#pragma mark TAB BAR DESIGN
/*struct {
NSInteger noOfTabs : 2;
UITabBarController *tabBarController;
NSString *deviceType;
} delegateResponse;
*/
@synthesize delegate;
UITabBarController *tabBarController;
- (void) setTabBarDesign{
NSInteger noOfTabs = [delegate noOfTabsForTabView];
tabBarController = [delegate setTabBarController];
buttonContainerView = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
0,
0)];
float width = 0.0f;
for (int i = 0; i < noOfTabs ; i++) {
//NSLog(@"%d", i);
UIButton *tabSection = [delegate tabView:self.view buttonAtIndex:i];
CGRect frame = tabSection.frame;
[tabSection setFrame:CGRectMake(i*frame.size.width,
0,
frame.size.width,
self.view.frame.size.height)];
width = width + frame.size.width;
//NSLog(@"tabSection.frame = %@ ---- tag =%d",NSStringFromCGRect(tabSection.frame), tabSection.tag);
[tabSection setUserInteractionEnabled:YES];
tabSection.tag = i+1;
[tabSection addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[tabSection setAutoresizingMask:UIViewAutoresizingNone];
if (i < noOfTabs) {
UIImageView *sepratorStrip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom Line.png"]];
[sepratorStrip setFrame:CGRectMake(tabSection.frame.origin.x + tabSection.frame.size.width + 1,
0,
1,
self.view.frame.size.height)];
[buttonContainerView addSubview:sepratorStrip];
}
[buttonContainerView addSubview:tabSection];
}
[buttonContainerView setBackgroundColor:[UIColor clearColor]];
[buttonContainerView setFrame:CGRectMake(0, 0, width, self.view.frame.size.height)];
[buttonContainerView setCenter:CGPointMake(self.view.frame.size.width / 2,
self.view.frame.size.height / 2)];
[buttonContainerView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin];
[self.view addSubview:buttonContainerView];
[self.view setAutoresizingMask: UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin];
}
- (void) onClick : (id) sender{
[self performSelector:@selector(deselectAll)];
[((UIButton *) sender) setSelected:YES];
[tabBarController setSelectedIndex:((UIButton *) sender).tag - 1];
if ([tabBarController.selectedViewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController*)tabBarController.selectedViewController popToRootViewControllerAnimated:YES];
}
}
- (void) deselectAll{
NSArray *views = [buttonContainerView subviews];
for (UIView *v in views) {
if ([v isKindOfClass:[UIButton class]]) {
[((UIButton *)v) setSelected:NO];
}
}
}
/*
- (void) setDelegate:(id <CustomTabBarDelegate>) theDelegate{
}
*/
- (void) setTabBarDesignWith : (UIImage *) backGroundImage
noOfTabs : (int) noOfTabs
tabsOnImages : (NSArray *) tabsOnImages
tabsOverImages : (NSArray *) tabsOverImages
tabsBackGroundImages : (NSArray *) tabsBackGroundImages
tabTitles : (NSArray *) tabTitles{
UIImageView *backGroundImageView = [[UIImageView alloc] initWithImage:backGroundImage];
[backGroundImageView setFrame:self.view.frame];
[self.view addSubview:backGroundImageView];
}
@end