Created
May 27, 2014 12:58
-
-
Save advantis/ec4a5bb2d371a4ee1f2d to your computer and use it in GitHub Desktop.
Generic UITabBarController replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright © 2014 Yuri Kotov | |
// | |
#import <UIKit/UIKit.h> | |
@interface ADVTabViewController : UIViewController | |
@property (nonatomic) NSUInteger selectedIndex; | |
@property (nonatomic, copy) NSArray *viewControllers; | |
@property (nonatomic, readonly) UIViewController *selectedViewController; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright © 2014 Yuri Kotov | |
// | |
#import "ADVTabViewController.h" | |
@interface ADVTabViewController () | |
@property (nonatomic, readwrite) UIViewController *selectedViewController; | |
@end | |
@implementation ADVTabViewController | |
- (void)setSelectedIndex:(NSUInteger)index { | |
if (index != _selectedIndex) { | |
_selectedIndex = index; | |
self.selectedViewController = self.viewControllers[index]; | |
} | |
} | |
- (void)setViewControllers:(NSArray *)viewControllers { | |
_selectedIndex = 0; | |
_viewControllers = [viewControllers copy]; | |
self.selectedViewController = [viewControllers firstObject]; | |
} | |
- (void)setSelectedViewController:(UIViewController *)controller { | |
[_selectedViewController willMoveToParentViewController:nil]; | |
[_selectedViewController.view removeFromSuperview]; | |
[_selectedViewController removeFromParentViewController]; | |
_selectedViewController = controller; | |
[self addChildViewController:controller]; | |
[self addChildView:controller.view]; | |
[controller didMoveToParentViewController:self]; | |
} | |
- (void)addChildView:(UIView *)subview { | |
subview.frame = self.view.bounds; | |
subview.translatesAutoresizingMaskIntoConstraints = YES; | |
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | |
[self.view addSubview:subview]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment