Skip to content

Commit 247f6ed

Browse files
OpenComOpenCom
OpenCom
authored and
OpenCom
committed
添加tabbar
1 parent a3f4974 commit 247f6ed

14 files changed

+349
-9
lines changed

动态的加载tabBar数量.xcodeproj/project.pbxproj

+30
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
4614378C1CB4E7E200641C04 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4614378A1CB4E7E200641C04 /* Main.storyboard */; };
1414
4614378E1CB4E7E200641C04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4614378D1CB4E7E200641C04 /* Assets.xcassets */; };
1515
461437911CB4E7E200641C04 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4614378F1CB4E7E200641C04 /* LaunchScreen.storyboard */; };
16+
4614379A1CB4E87700641C04 /* oneViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 461437991CB4E87700641C04 /* oneViewController.m */; };
17+
4614379D1CB4E88700641C04 /* twoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4614379C1CB4E88700641C04 /* twoViewController.m */; };
18+
461437A01CB4E89300641C04 /* threeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4614379F1CB4E89300641C04 /* threeViewController.m */; };
19+
461437A31CB4E8A000641C04 /* fourViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 461437A21CB4E8A000641C04 /* fourViewController.m */; };
20+
461437A61CB4E8BD00641C04 /* TabBarViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 461437A51CB4E8BD00641C04 /* TabBarViewController.m */; };
1621
/* End PBXBuildFile section */
1722

1823
/* Begin PBXFileReference section */
@@ -26,6 +31,16 @@
2631
4614378D1CB4E7E200641C04 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2732
461437901CB4E7E200641C04 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2833
461437921CB4E7E200641C04 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
34+
461437981CB4E87700641C04 /* oneViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = oneViewController.h; sourceTree = "<group>"; };
35+
461437991CB4E87700641C04 /* oneViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = oneViewController.m; sourceTree = "<group>"; };
36+
4614379B1CB4E88700641C04 /* twoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = twoViewController.h; sourceTree = "<group>"; };
37+
4614379C1CB4E88700641C04 /* twoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = twoViewController.m; sourceTree = "<group>"; };
38+
4614379E1CB4E89300641C04 /* threeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = threeViewController.h; sourceTree = "<group>"; };
39+
4614379F1CB4E89300641C04 /* threeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = threeViewController.m; sourceTree = "<group>"; };
40+
461437A11CB4E8A000641C04 /* fourViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fourViewController.h; sourceTree = "<group>"; };
41+
461437A21CB4E8A000641C04 /* fourViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = fourViewController.m; sourceTree = "<group>"; };
42+
461437A41CB4E8BD00641C04 /* TabBarViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TabBarViewController.h; sourceTree = "<group>"; };
43+
461437A51CB4E8BD00641C04 /* TabBarViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TabBarViewController.m; sourceTree = "<group>"; };
2944
/* End PBXFileReference section */
3045

3146
/* Begin PBXFrameworksBuildPhase section */
@@ -60,8 +75,18 @@
6075
children = (
6176
461437841CB4E7E200641C04 /* AppDelegate.h */,
6277
461437851CB4E7E200641C04 /* AppDelegate.m */,
78+
461437A41CB4E8BD00641C04 /* TabBarViewController.h */,
79+
461437A51CB4E8BD00641C04 /* TabBarViewController.m */,
6380
461437871CB4E7E200641C04 /* ViewController.h */,
6481
461437881CB4E7E200641C04 /* ViewController.m */,
82+
461437981CB4E87700641C04 /* oneViewController.h */,
83+
461437991CB4E87700641C04 /* oneViewController.m */,
84+
4614379B1CB4E88700641C04 /* twoViewController.h */,
85+
4614379C1CB4E88700641C04 /* twoViewController.m */,
86+
4614379E1CB4E89300641C04 /* threeViewController.h */,
87+
4614379F1CB4E89300641C04 /* threeViewController.m */,
88+
461437A11CB4E8A000641C04 /* fourViewController.h */,
89+
461437A21CB4E8A000641C04 /* fourViewController.m */,
6590
4614378A1CB4E7E200641C04 /* Main.storyboard */,
6691
4614378D1CB4E7E200641C04 /* Assets.xcassets */,
6792
4614378F1CB4E7E200641C04 /* LaunchScreen.storyboard */,
@@ -150,8 +175,13 @@
150175
buildActionMask = 2147483647;
151176
files = (
152177
461437891CB4E7E200641C04 /* ViewController.m in Sources */,
178+
461437A31CB4E8A000641C04 /* fourViewController.m in Sources */,
153179
461437861CB4E7E200641C04 /* AppDelegate.m in Sources */,
154180
461437831CB4E7E200641C04 /* main.m in Sources */,
181+
461437A61CB4E8BD00641C04 /* TabBarViewController.m in Sources */,
182+
461437A01CB4E89300641C04 /* threeViewController.m in Sources */,
183+
4614379A1CB4E87700641C04 /* oneViewController.m in Sources */,
184+
4614379D1CB4E88700641C04 /* twoViewController.m in Sources */,
155185
);
156186
runOnlyForDeploymentPostprocessing = 0;
157187
};

动态的加载tabBar数量/AppDelegate.m

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,31 @@
77
//
88

99
#import "AppDelegate.h"
10-
10+
#import "TabBarViewController.h"
1111
@interface AppDelegate ()
1212

1313
@end
1414

1515
@implementation AppDelegate
16-
16+
/**
17+
* 动态的加载tabbar (注意系统自带的tabbar最多只能有5个item)
18+
* 方案一:从服务器获取一个数组这个数组中存储着tabbar的item模型(名字、图片名、子控制器类型)
19+
* 方案二:从服务器获取一个JSON数据,解析JSON数据,根据该数据来设置tabbar
20+
*/
1721

1822
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19-
// Override point for customization after application launch.
23+
24+
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
25+
26+
// 这里抽取一个工具类,来实现这个逻辑操作。
27+
TabBarViewController *tabVc = [[TabBarViewController alloc]init];
28+
29+
tabVc.titleArr = @[@"1",@"2",@"1",@"2",@"1",@"2"];
30+
31+
self.window.rootViewController = tabVc;
32+
33+
[self.window makeKeyAndVisible];
34+
2035
return YES;
2136
}
2237

动态的加载tabBar数量/Info.plist

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
<true/>
2525
<key>UILaunchStoryboardName</key>
2626
<string>LaunchScreen</string>
27-
<key>UIMainStoryboardFile</key>
28-
<string>Main</string>
2927
<key>UIRequiredDeviceCapabilities</key>
3028
<array>
3129
<string>armv7</string>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// TabBarViewController.h
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface TabBarViewController : UITabBarController
12+
@property (nonatomic, strong) NSArray *titleArr;
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// TabBarViewController.m
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import "TabBarViewController.h"
10+
#import "ViewController.h"
11+
#import "oneViewController.h"
12+
#import "twoViewController.h"
13+
#import "threeViewController.h"
14+
#import "fourViewController.h"
15+
16+
17+
@interface TabBarViewController ()
18+
@end
19+
20+
@implementation TabBarViewController
21+
22+
- (void)setTitleArr:(NSArray *)titleArr
23+
{
24+
_titleArr = titleArr;
25+
for (int i = 0; i < self.titleArr.count; i++) {
26+
UIViewController *vc = [[ViewController alloc]init];
27+
vc.title = [NSString stringWithFormat:@"%d个控制器",i];
28+
UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:vc];
29+
[self addChildViewController:na];
30+
}
31+
}
32+
33+
- (void)viewDidLoad {
34+
[super viewDidLoad];
35+
36+
UITabBar *tabBar = [UITabBar appearance];
37+
[tabBar setBarTintColor:[UIColor orangeColor]];
38+
39+
}
40+
41+
- (void)setupChildVc
42+
{
43+
NSLog(@"%ld",self.titleArr.count);
44+
for (int i = 0; i < self.titleArr.count; i++) {
45+
46+
UIViewController *vc = [[ViewController alloc]init];
47+
vc.title = [NSString stringWithFormat:@"%d个控制器",i];
48+
[self addChildViewController:vc];
49+
}
50+
51+
// UIViewController *vc1 = [[ViewController alloc]init];
52+
// UIViewController *vc2 = [[oneViewController alloc]init];
53+
// UIViewController *vc3 = [[twoViewController alloc]init];
54+
// UIViewController *vc4 = [[threeViewController alloc]init];
55+
// UIViewController *vc5 = [[fourViewController alloc]init];
56+
//
57+
// vc1.title = @"第一个";
58+
// vc2.title = @"第二个";
59+
// vc3.title = @"第三个";
60+
// vc4.title = @"第四个";
61+
// vc5.title = @"第五个";
62+
//
63+
// [self addChildViewController:vc1];
64+
// [self addChildViewController:vc2];
65+
// [self addChildViewController:vc3];
66+
// [self addChildViewController:vc4];
67+
// [self addChildViewController:vc5];
68+
}
69+
70+
@end

动态的加载tabBar数量/ViewController.m

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,34 @@
88

99
#import "ViewController.h"
1010

11-
@interface ViewController ()
11+
#define KVColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
12+
#define KVRandomColor KVColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
1213

14+
@interface ViewController ()
1315
@end
1416

1517
@implementation ViewController
16-
18+
+ (void)load
19+
{
20+
UINavigationBar *navigationBar = [UINavigationBar appearance];
21+
[navigationBar setBarTintColor:[UIColor redColor]];
22+
}
23+
- (void)viewWillAppear:(BOOL)animated
24+
{
25+
UINavigationBar *navigationBar = [UINavigationBar appearance];
26+
[navigationBar setBarTintColor:[UIColor redColor]];
27+
}
1728
- (void)viewDidLoad {
1829
[super viewDidLoad];
19-
// Do any additional setup after loading the view, typically from a nib.
20-
}
2130

31+
self.view.backgroundColor = KVRandomColor;
32+
33+
}
34+
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
35+
{
36+
UINavigationBar *navigationBar = [UINavigationBar appearance];
37+
[navigationBar setBarTintColor:[UIColor whiteColor]];
38+
}
2239
- (void)didReceiveMemoryWarning {
2340
[super didReceiveMemoryWarning];
2441
// Dispose of any resources that can be recreated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// fourViewController.h
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface fourViewController : UIViewController
12+
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// fourViewController.m
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import "fourViewController.h"
10+
11+
@interface fourViewController ()
12+
13+
@end
14+
15+
@implementation fourViewController
16+
17+
- (void)viewDidLoad {
18+
[super viewDidLoad];
19+
self.view.backgroundColor = [UIColor greenColor];
20+
}
21+
22+
- (void)didReceiveMemoryWarning {
23+
[super didReceiveMemoryWarning];
24+
// Dispose of any resources that can be recreated.
25+
}
26+
27+
/*
28+
#pragma mark - Navigation
29+
30+
// In a storyboard-based application, you will often want to do a little preparation before navigation
31+
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
32+
// Get the new view controller using [segue destinationViewController].
33+
// Pass the selected object to the new view controller.
34+
}
35+
*/
36+
37+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// oneViewController.h
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface oneViewController : UIViewController
12+
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// oneViewController.m
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import "oneViewController.h"
10+
11+
@interface oneViewController ()
12+
13+
@end
14+
15+
@implementation oneViewController
16+
17+
- (void)viewDidLoad {
18+
[super viewDidLoad];
19+
self.view.backgroundColor = [UIColor orangeColor];
20+
}
21+
- (void)didReceiveMemoryWarning {
22+
[super didReceiveMemoryWarning];
23+
// Dispose of any resources that can be recreated.
24+
}
25+
26+
/*
27+
#pragma mark - Navigation
28+
29+
// In a storyboard-based application, you will often want to do a little preparation before navigation
30+
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
31+
// Get the new view controller using [segue destinationViewController].
32+
// Pass the selected object to the new view controller.
33+
}
34+
*/
35+
36+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// threeViewController.h
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface threeViewController : UIViewController
12+
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// threeViewController.m
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import "threeViewController.h"
10+
11+
@interface threeViewController ()
12+
13+
@end
14+
15+
@implementation threeViewController
16+
17+
- (void)viewDidLoad {
18+
[super viewDidLoad];
19+
self.view.backgroundColor = [UIColor redColor];
20+
}
21+
- (void)didReceiveMemoryWarning {
22+
[super didReceiveMemoryWarning];
23+
// Dispose of any resources that can be recreated.
24+
}
25+
26+
/*
27+
#pragma mark - Navigation
28+
29+
// In a storyboard-based application, you will often want to do a little preparation before navigation
30+
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
31+
// Get the new view controller using [segue destinationViewController].
32+
// Pass the selected object to the new view controller.
33+
}
34+
*/
35+
36+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// twoViewController.h
3+
// 动态的加载tabBar数量
4+
//
5+
// Created by OpenCom on 16/4/6.
6+
// Copyright © 2016年 OpenCom. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface twoViewController : UIViewController
12+
13+
@end

0 commit comments

Comments
 (0)