Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buttons property exposed #26

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MHCustomTabBarController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'MHCustomTabBarController'
s.version = '1.2.1'
s.version = '1.3.0'
s.platform = :ios
s.ios.deployment_target = '6.0'
s.summary = 'Custom UITabBar replacement using storyboard and segues.'
Expand All @@ -11,8 +11,8 @@ Pod::Spec.new do |s|
'Martin Hartl' => '[email protected]'
}
s.source = {
:git => 'https://github.com/mhaddl/MHCustomTabBarController.git',
:tag => 'v1.2.1'
:git => 'https://github.com/schmidt9/MHCustomTabBarController.git',
:tag => 'v1.3.0'
}
s.source_files = 'MHCustomTabBarController/*.{m,h}'
end
27 changes: 26 additions & 1 deletion MHCustomTabBarController/MHCustomTabBarController.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,32 @@ extern NSString *const MHCustomTabBarControllerViewControllerAlreadyVisibleNotif
@property (weak,nonatomic) UIViewController *destinationViewController;
@property (strong, nonatomic) UIViewController *oldViewController;
@property (weak, nonatomic) IBOutlet UIView *container;
@property (nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

@property (assign, nonatomic) NSInteger selectedIndex;
@property (nonatomic, readonly) NSInteger selectedIndex;
/**
* Button to be selected initially,
* set it in subclass before [super viewWillAppear:] call
*/
@property (nonatomic) UIButton *initialSelectedButton;
/**
* Segue identifier for view controller that should be opened initially,
* set it in subclass before [super viewWillAppear:] call
*/
@property (nonatomic) NSString *initialSegueIdentifier;
/**
* Do not clear previous selection
*/
@property (nonatomic) BOOL keepsSelection;
/**
* Replace old view controller with new one.
* self.childViewControllers will contain only one (current) view controller in this case
*/
@property (nonatomic) BOOL replacesOldViewController;
/**
* Defaults to YES, If NO behave as ordinary view controller (not using custom segues logic)
*/
@property (nonatomic) BOOL usesTabs;

@end
70 changes: 61 additions & 9 deletions MHCustomTabBarController/MHCustomTabBarController.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,52 @@
NSString *const MHCustomTabBarControllerViewControllerChangedNotification = @"MHCustomTabBarControllerViewControllerChangedNotification";
NSString *const MHCustomTabBarControllerViewControllerAlreadyVisibleNotification = @"MHCustomTabBarControllerViewControllerAlreadyVisibleNotification";

NSString *const kDefaultSegueIdentifier = @"viewController1";


@interface MHCustomTabBarController ()

@property (nonatomic, strong) NSMutableDictionary *viewControllersByIdentifier;
@property (strong, nonatomic) NSString *destinationIdentifier;
@property (nonatomic) IBOutletCollection(UIButton) NSArray *buttons;

@end


@implementation MHCustomTabBarController

- (void)viewDidLoad {
[super viewDidLoad];

self.viewControllersByIdentifier = [NSMutableDictionary dictionary];
self.replacesOldViewController = YES;
self.usesTabs = YES;
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

if (!self.usesTabs) {
return;
}

assert(self.buttons || self.segmentedControl);

id sender;

if (self.buttons) {
sender = self.initialSelectedButton
? self.initialSelectedButton
: self.buttons.firstObject;
} else {
sender = self.segmentedControl;
}

if (!self.initialSegueIdentifier) {
self.initialSegueIdentifier = kDefaultSegueIdentifier;
}

if (self.childViewControllers.count < 1) {
[self performSegueWithIdentifier:@"viewController1" sender:[self.buttons objectAtIndex:0]];
[self performSegueWithIdentifier:self.initialSegueIdentifier sender:sender];
}
}

Expand All @@ -58,29 +83,50 @@ - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInte
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if (![segue isKindOfClass:[MHTabBarSegue class]]) {
if (!self.usesTabs || ![segue isKindOfClass:[MHTabBarSegue class]]) {
[super prepareForSegue:segue sender:sender];
return;
}

((MHTabBarSegue *)segue).replacesOldViewController = self.replacesOldViewController;

self.oldViewController = self.destinationViewController;

//if view controller isn't already contained in the viewControllers-Dictionary
if (![self.viewControllersByIdentifier objectForKey:segue.identifier]) {
[self.viewControllersByIdentifier setObject:segue.destinationViewController forKey:segue.identifier];
if (!self.viewControllersByIdentifier[segue.identifier]) {
self.viewControllersByIdentifier[segue.identifier] = segue.destinationViewController;
}

[self.buttons setValue:@NO forKeyPath:@"selected"];
[sender setSelected:YES];
self.selectedIndex = [self.buttons indexOfObject:sender];
assert(self.buttons || self.segmentedControl);

if (self.buttons) {
for (UIButton *btn in self.buttons) {
if (self.keepsSelection) {
continue;
}

btn.selected = NO;
}

[sender setSelected:YES];
_selectedIndex = [self.buttons indexOfObject:sender];
} else {
[sender setSelected:YES];
_selectedIndex = self.segmentedControl.selectedSegmentIndex;
}

self.destinationIdentifier = segue.identifier;
self.destinationViewController = [self.viewControllersByIdentifier objectForKey:self.destinationIdentifier];
self.destinationViewController = self.viewControllersByIdentifier[self.destinationIdentifier];

[[NSNotificationCenter defaultCenter] postNotificationName:MHCustomTabBarControllerViewControllerChangedNotification object:nil];
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {

if (!self.usesTabs) {
return [super shouldPerformSegueWithIdentifier:identifier sender:sender];
}

if ([self.destinationIdentifier isEqual:identifier]) {
//Dont perform segue, if visible ViewController is already the destination ViewController
[[NSNotificationCenter defaultCenter] postNotificationName:MHCustomTabBarControllerViewControllerAlreadyVisibleNotification object:nil];
Expand All @@ -92,6 +138,12 @@ - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sende

#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning {

if (!self.usesTabs) {
[super didReceiveMemoryWarning];
return;
}

[[self.viewControllersByIdentifier allKeys] enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) {
if (![self.destinationIdentifier isEqualToString:key]) {
[self.viewControllersByIdentifier removeObjectForKey:key];
Expand Down
2 changes: 2 additions & 0 deletions MHCustomTabBarController/MHTabBarSegue.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@

@interface MHTabBarSegue : UIStoryboardSegue

@property (nonatomic) BOOL replacesOldViewController;

@end
28 changes: 24 additions & 4 deletions MHCustomTabBarController/MHTabBarSegue.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,40 @@

@implementation MHTabBarSegue

- (instancetype)initWithIdentifier:(nullable NSString *)identifier
source:(UIViewController *)source
destination:(UIViewController *)destination
{
if ((self = [super initWithIdentifier:identifier source:source destination:destination])) {
self.replacesOldViewController = YES;
}
return self;
}

- (void)perform {
MHCustomTabBarController *tabBarViewController = (MHCustomTabBarController *)self.sourceViewController;
UIViewController *destinationViewController = (UIViewController *)tabBarViewController.destinationViewController;
UIViewController *destinationViewController = tabBarViewController.destinationViewController;

//remove old viewController
if (tabBarViewController.oldViewController) {
if (tabBarViewController.oldViewController && self.replacesOldViewController) {
[tabBarViewController.oldViewController willMoveToParentViewController:nil];
[tabBarViewController.oldViewController.view removeFromSuperview];
[tabBarViewController.oldViewController removeFromParentViewController];
}

destinationViewController.view.frame = tabBarViewController.container.bounds;
[tabBarViewController addChildViewController:destinationViewController];
[tabBarViewController.container addSubview:destinationViewController.view];

UIView *view = destinationViewController.view;
view.translatesAutoresizingMaskIntoConstraints = NO;

UIView *container = tabBarViewController.container;
[container addSubview:view];

[view.leadingAnchor constraintEqualToAnchor:container.leadingAnchor].active = YES;
[view.trailingAnchor constraintEqualToAnchor:container.trailingAnchor].active = YES;
[view.topAnchor constraintEqualToAnchor:container.topAnchor].active = YES;
[view.bottomAnchor constraintEqualToAnchor:container.bottomAnchor].active = YES;

[destinationViewController didMoveToParentViewController:tabBarViewController];
}

Expand Down