建立自己的TabBarController,继承UITabBarController
MyTabBarController.m
- (void)viewDidLoad {
[super viewDidLoad];
// 使用系统自带的tabBar注意这两个函数的先后顺序
[self createViewControllers];
[self createMyTabBarItem];
}
- (void)btnClick:(UIButton *)sender {
// 设置视图切换
NSInteger index = sender.tag-100;
self.selectedIndex = index;
for (UIView * view in self.tabBar.subviews) {
// 要判断view的类型
if ([view isKindOfClass:[UIButton class]]) {
UIButton * btn = (UIButton *)view;
if (btn.tag == sender.tag) {
btn.selected = YES;
} else {
btn.selected = NO;
}
}
}
/*
self.tabBar.subviews.count = 5;
self.tabBar.subviews:
(
"<_UITabBarBackgroundView: 0x7fc513ff48c0; frame = (0 0; 375 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fc513ff4c40>>",
"<UIButton: 0x7fc513e238e0; frame = (56.25 0; 50 49); opaque = NO; tag = 100; layer = <CALayer: 0x7fc513e23d40>>",
"<UIButton: 0x7fc513fda4b0; frame = (162.5 0; 50 49); opaque = NO; tag = 101; layer = <CALayer: 0x7fc513fda6d0>>",
"<UIButton: 0x7fc513fe4520; frame = (268.75 0; 50 49); opaque = NO; tag = 102; layer = <CALayer: 0x7fc513fe1920>>",
"<UIImageView: 0x7fc513ff5ab0; frame = (0 -0.5; 375 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fc513ff5720>>"
)
*/
}
- (void)createViewControllers {
NSMutableArray * viewControllers = [[NSMutableArray alloc] init];
// NSArray * classNames = @[@"MovieViewController",@"CinemaViewController",@"SettingViewController"];
// for (NSInteger i=0; i<classNames.count; i++) {
// //将字符串表示的类名转换为类(对象)
// Class class = NSClassFromString(classNames[i]);
// BaseViewController * bVC = [[class alloc] init];
//
// UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:bVC];
// [viewControllers addObject:nav];
//
// }
MovieViewController * MVC = [[MovieViewController alloc] init];
[MVC becomeObserve];
UINavigationController * Mnav = [[UINavigationController alloc] initWithRootViewController:MVC];
[viewControllers addObject:Mnav];
CinemaViewController * CVC = [[CinemaViewController alloc] init];
[CVC becomeObserve];
UINavigationController * Nnav = [[UINavigationController alloc] initWithRootViewController:CVC];
[viewControllers addObject:Nnav];
SettingViewController * SVC = [[SettingViewController alloc] init];
UINavigationController * Snav = [[UINavigationController alloc] initWithRootViewController:SVC];
[viewControllers addObject:Snav];
self.viewControllers = viewControllers;
}