你这样做非常非常错误。当您可以只使用默认视图层次结构时,不要创建自定义视图层次结构。
您要做的是创建一个子类UITabBarController
并创建一个 .xib 文件,该文件将包含您的自定义标签栏 - 只是一个图像和任意数量的 UIButtons(我想 5 个)。
为所有这些设置标签,只有 1-5 个标签,您可以使用自定义UIView
子类来完成,但在这种情况下这将是多余的,所以它只是获取带有标签的控件。
创建一个子类UITabBarController
。您需要引用所有这些按钮以及查看最后按下哪个按钮的属性,以便您可以适当地更新 UI。还为不同的控制状态分配不同的图像或标题,在这种情况下我使用默认并选择。
MYBaseTabBarController.h
@interface MYBaseTabBarController : UITabBarController
@property (strong, nonatomic) UIButton *btn1;
@property (strong, nonatomic) UIButton *btn2;
@property (strong, nonatomic) UIButton *btn3;
@property (strong, nonatomic) UIButton *btn4;
@property (strong, nonatomic) UIButton *btn5;
@property (weak, nonatomic) UIButton *lastSender;
@property (strong, nonatomic) UIView *tabBarView;
@end
MYBaseTabBarController.m
首先,创建视图控制器(UINavigationController
在这种情况下都是子类)并将它们UITabBarController
作为viewControllers
属性分配给您的子类。
- (id)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
NSMutableArray *viewControllers = [NSMutableArray array];
MYViewController1 *viewController1 = [[MYStoryboardManager storyboard1] instantiateInitialViewController];
viewController1.title = @"1";
[viewControllers addObject:viewController1];
MYViewController2 *viewController2 = [[MYStoryboardManager storyboard2] instantiateInitialViewController];
viewController2.title = @"2";
[viewControllers addObject:viewController2];
UIViewController *blankController = [UIViewController new]; // Center button, performs an action instead of leading to a controller
[viewControllers addObject:blankController];
MYViewController3 *viewController3 = [[MYStoryboardManager storyboard3] instantiateInitialViewController];
viewController3.title = @"3";
[viewControllers addObject:viewController3];
MYViewController3 *viewController4 = [[MYStoryboardManager storyboard4] instantiateInitialViewController];
viewController4.title = @"4";
[viewControllers addObject:viewController4];
self.viewControllers = viewControllers;
}
接下来获取您之前创建的按钮并在-viewDidLoad
方法中为它们分配操作:
- (void)viewDidLoad {
[super viewDidLoad];
_tabbarView = [[[NSBundle mainBundle] loadNibNamed:@"MyTabBar" owner:nil options:nil] lastObject]; // "MyTabBar" is the name of the .xib file
_tabbarView.frame = CGRectMake(0.0,
self.view.frame.size.height - _tabbarView.frame.size.height,
_tabbarView.frame.size.width,
_tabbarView.frame.size.height); // make it overlay your actual tab bar
[self.view addSubview:_tabbarView];
_btn1 = (UIButton *)[_tabbarView viewWithTag:1];
[_btn1 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
_btn2 = (UIButton *)[_tabbarView viewWithTag:2];
[_btn2 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
_btn3 = (UIButton *)[_tabbarView viewWithTag:3];
[_btn3 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
_btn4 = (UIButton *)[_tabbarView viewWithTag:4];
[_btn4 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
_btn5 = (UIButton *)[_tabbarView viewWithTag:5];
[_btn5 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
_lastSender = _btn1;
[self setSelectedViewController:self.viewControllers[0]]; // make first controller selected
}
添加处理方法:
- (void)processBtn:(UIButton *)sender {
_lastSender = sender;
[self setSelectedViewController:[self.viewControllers objectAtIndex:sender.tag - 1]];
}
最后覆盖该-setSelectedViewController:
方法:
- (void)setSelectedViewController:(UIViewController *)selectedViewController {
if (_lastSender != _btn3) { // check if it's not the action button
for (UIButton *btn in [_tabbarView subviews]) {
if ([btn isKindOfClass:[UIButton class]]) {
if (btn == _lastSender) {
btn.selected = YES;
}
else {
btn.selected = NO;
}
}
}
}
if ([self.viewControllers indexOfObject:selectedViewController] == 2) {
MYActionController *viewController = [[MYStoryboardManager actionStoryboard] instantiateInitialViewController];
[self presentViewController:viewController animated:YES completion:nil];
}
else {
if (self.selectedViewController == selectedViewController) {
[(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:animate]; // pop to root if tapped the same controller twice
}
[super setSelectedViewController:selectedViewController];
}
}
我假设您在启用 ARC 的情况下进行编程,并且您有一个管理故事板的类,但无论如何这非常简单。