Skip to content

Instantly share code, notes, and snippets.

@dr2050
Last active August 29, 2015 14:16
Show Gist options
  • Save dr2050/61f404f50806b24b13ab to your computer and use it in GitHub Desktop.
Save dr2050/61f404f50806b24b13ab to your computer and use it in GitHub Desktop.
Drop in replacement for UITabBarController that plays well with UINavigationController as parent
//
// Created by Dan Rosenstark on 2/28/15.
// Copyright (c) 2015 Confusion Studios LLC. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DRTabBarController : UIViewController <UITabBarDelegate>;
@property (nonatomic, strong) NSArray *viewControllers;
@property (nonatomic, strong) UITabBar *tabBar;
@property (nonatomic, strong) UIView *mainView;
@end
//
// Created by dr2050 on 2/28/15.
// Copyright (c) 2015 Confusion Studios LLC. All rights reserved.
//
#import "DRTabBarController.h"
@implementation DRTabBarController
- (instancetype)init {
self = [super init];
if (self) {
self.tabBar = [[UITabBar alloc] init];
self.tabBar.delegate = self;
self.mainView = [[UIView alloc] init];
}
return self;
}
- (void)viewDidLoad {
[self.view addSubview:self.tabBar];
[self.view addSubview:self.mainView];
}
- (void)setViewControllers:(NSArray *)viewControllers {
_viewControllers = viewControllers;
NSMutableArray *tabBarItems = [NSMutableArray array];
for (UIViewController *controller in viewControllers) {
UITabBarItem *item = controller.tabBarItem;
[tabBarItems addObject:item];
}
self.tabBar.items = tabBarItems;
}
- (void)viewWillAppear:(BOOL)animated {
[self.tabBar setSelectedItem:self.tabBar.items.firstObject];
[self tabBar:self.tabBar didSelectItem:self.tabBar.items.firstObject];
}
-(void)viewDidLayoutSubviews {
CGRect frame = self.view.bounds;
UITabBarController *throwaway = [[UITabBarController alloc] init];
frame.size.height = throwaway.tabBar.frame.size.height;
frame.origin.y = self.view.bounds.size.height - frame.size.height;
self.tabBar.frame = frame;
self.tabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
frame = self.view.bounds;
frame.size.height -= self.tabBar.frame.size.height;
float navbarHeight = self.navigationController.navigationBar.frame.size.height;
// WARNING: MAGIC NUMBER FOR STATUS BAR HEIGHT!
// cannot use UIApplication.sharedApplication.statusBarFrame.size.height because
// reports are not right with in-call status bar
float statusBarHeight = UIApplication.sharedApplication.statusBarHidden ? 0 : 20;
float topBarHeight = navbarHeight + statusBarHeight;
frame.origin.y += topBarHeight;
frame.size.height -= topBarHeight;
self.mainView.frame = frame;
self.mainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSUInteger index = [self.tabBar.items indexOfObject:item];
UIView *view = [[self.viewControllers objectAtIndex:index] view];
view.frame = self.mainView.bounds;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.mainView addSubview:view];
[self.mainView bringSubviewToFront:view];
}
@end
@dr2050
Copy link
Author

dr2050 commented Mar 2, 2015

The magic variable, 20, is used instead of status bar height because the in-call status bar messes things up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment