Skip to content

Instantly share code, notes, and snippets.

@StefanLage
Last active October 23, 2015 14:34
Show Gist options
  • Save StefanLage/edc7196e139c76d5ed6e to your computer and use it in GitHub Desktop.
Save StefanLage/edc7196e139c76d5ed6e to your computer and use it in GitHub Desktop.
Hide selected controllers you don't want to appear in a UITabBarController from the Storyboard
#import <UIKit/UIKit.h>
@interface UITabBarController (HideViews)
@property (nonatomic, strong) NSString *hideViews;
@end
#import "UITabBarController+HideViews.h"
static NSString *syntaxSeparator = @",";
@implementation UITabBarController (HideViews)
@dynamic hideViews;
/**
* Setter of hideViews User Defined Runtime Attributes
*
* @param hideViews, list of all Controller that we should hide / remove
* hideViews's format : ViewController1,ViewController2,...
*/
-(void)setHideViews:(NSString *)hideViews{
// Do something if there are some childs
if(self.viewControllers){
NSMutableArray<__kindof UIViewController *> * _Nullable viewControllers = (NSMutableArray*)self.viewControllers;
NSMutableSet *controllerToHide = [NSMutableSet new];
// Add all controllers to hide in a set (if there is a class corresponding to them)
for(NSString *viewToHide in [hideViews componentsSeparatedByString:syntaxSeparator])
if(NSClassFromString(viewToHide))
[controllerToHide addObject:NSClassFromString(viewToHide)];
if(controllerToHide.count > 0){
// Find out all controller wanted
for(UIViewController *vc in self.viewControllers){
__weak UIViewController *vcCopy = vc;
// We don't want to hide an UINavigationController, except whether the user said so
if([vc isKindOfClass:[UINavigationController class]] && ![controllerToHide containsObject:[vc class]])
vcCopy = [(UINavigationController*)vc topViewController];
// Remove it
if([controllerToHide containsObject:[vcCopy class]])
[viewControllers removeObject:vc];
}
// Update itself
[self setViewControllers:viewControllers];
}
}
}
@end
@StefanLage
Copy link
Author

Adding this UITabBarController's category in your project, you can avoid some views you don't want to display in your app (for any reasons).
Then to define which controller you don't want to take part of the journey, just define a runtime attribute on the UITabBarController you want and name it "hideViews", see the example below:

uitabbarcontroller hideviews

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