ECSlidingViewController with UITabBarController - ios

I have a requirement where I need to slide the view but thats inside a UITabBarController.
Does any one tried doing that ? ECSlidingViewController works perfect if I make it rootViewController but I want that in UITabBarController.
I tried setting the ECSlidingViewController as one of the view controllers for the UITabBarController but it keeps crashing after the first swipe or some times it goes in infinite loop
Below is my code
myWorkController = [[TaskWorkViewController alloc] initWithNibName:#"WorkViewController_iPhone" bundle:nil]; // This is the controller where I have my specific logic
myWorkNavController = [[[UINavigationController alloc] initWithRootViewController:myWorkController] autorelease]; // setting the myWorkController to UINavigationController as I need to navigate from this view controller to different view on specific actions
//SlidingviewController setup
self.slidingViewController = [ECSlidingViewController slidingWithTopViewController:myWorkNavController];
self.slidingViewController.topViewController = myWorkNavController;
[myWorkNavController.view addGestureRecognizer:self.slidingViewController.panGesture];
self.slidingViewController.anchorRightPeekAmount = 100.0;
myWorkController.slidingViewController = self.slidingViewController;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = #[ self.slidingViewController, createNavController, currentWorkNavController];
and I am setting the underRightViewController inside myWorkController
if (self.slidingViewController != nil)
{
UIViewController *underRightViewController = [[[UIViewController alloc] init] autorelease];
// configure under right view controller
underRightViewController.view.layer.borderWidth = 0;
underRightViewController.view.layer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
underRightViewController.view.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
underRightViewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom | UIRectEdgeRight; // don't go under the top view
self.slidingViewController.underRightViewController = underRightViewController;
}
1) First swipe works perfect but the second swipe breaks in ECSlidingViewController code
2) As I am using UINavigationController, when I try to change underRightViewController it fails at [oldUnderRightViewController removeFromParentViewController]; of ECSlidingViewController code
Can some one please guide me what I am doing wrong or if this is possible with ECSlidingViewController
Thanks in advance

Related

Using UITabBar and UINavigationBar

I'm facing problem with using UITabBar and UINavigationBar at the same time. My intention is to have tabBar displaying my 3 tabs and navBar on each of these tabs displaying custom name and on some of these tabs additionally displaying some buttons (such as Add button).
I created view controllers in AppDelegate's didFinishLaunchingWithOptions:
AAA *vc1 = [[AAA alloc] init];
BBB *vc2 = [[BBB alloc] init];
CCC *vc3 = [[CCC alloc] init];
Created tabBar and populated it:
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[vc1, vc2, vc3];
Next I have added some titles and images to the tabBarItems of these view controllers which is all working fine, but than I wanted to display navigation bar on top of the application. So I created navigation controller, but I don't know what view controller I need to initiate it with. If I use vc1 and set navController as rootViewController, application displays vc1's view and shows navBar, but doesn't show tabBar.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc1];
self.window.rootViewController = navController;
//self.window.rootViewController = self.tabBarController;
I tried to set tabBarController to init navController and set navController as rootViewController - in this case views, tabBar and navBAr display correctly, but I don't have navBarItems associated with views vc1, vc2, vc3.
This is how I created navBarItems (e.g. in AAA.m):
- (instancetype)init {
self = [super init];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = #"name";
}
return self;
}
What do I need to do to make it work all together? Thanks.
EDIT:
I made some modification to the code and now navigation bar is visible, but only on the view controller set in UINavigationController's initWithRootViewController method. For example in following code I can see tab bar on every view and nav bar on the vc2 only. And my intention is to have tab bar and nav bar on every of these three vc.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:vc1, navController, vc3, nil];
self.window.rootViewController = self.tabBarController;
Ok, I have an approach for you...for this you need make changes in storyboard.
Here is a screenshot
Now create a class for UITabBar: class TabBarViewController: UITabBarController
Do not write anything in this class, it just for our reference.
In AppDelegate Class, method: didlaunchFinish:
let vcTabBarViewController = STORYBOARD.instantiateViewController(withIdentifier: "TabBarViewController") as? TabBarViewController
navigationController.viewControllers = [vcTabBarViewController!]
Now in your first view controller which is on 1st tab,
In viewDidLoad method:
paste these lines:
let tabBar = self.tabBarController?.tabBar
let homeTab = tabBar?.items?[0] as UITabBarItem!
let searchTab = tabBar?.items?[1] as UITabBarItem!
let myProfileTab = tabBar?.items?[4] as UITabBarItem!
homeTab?.image = UIImage(named: "home-select")!.withRenderingMode(.alwaysOriginal)
homeTab?.image = UIImage(named: "home")!.withRenderingMode(.alwaysTemplate)
searchTab?.image = UIImage(named: "search-select")!.withRenderingMode(.alwaysOriginal)
searchTab?.image = UIImage(named: "search")!.withRenderingMode(.alwaysTemplate)
I hope this will resolve your problem.
add these line of code on every controller
[self.tabBarController setTitle:#"xyz"];
self.tabBarController.navigationController.navigationBarHidden=NO;

UITabBarController with unselected tab

I have a view controller say 'HomeViewController' on which I have added a UITabBarController, which has 4 UITabBarItems. Now what I want is, when I select the HomeViewController no tab should should be selected for the first time and another view should be loaded. I have tried UITabBar instead of UITabBarController but its not working. I have also tried :
tabBarController.selectedViewController=nil; //giving crash
tabBarController.selectedIndex=-1; //not working either
UITabBarItems should be selected when user will click on them.
You have to add UITabBar object on HomeViewController.
In ViewDidLoad you have to add following code:
ViewController1 *controller1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
ViewController2 *controller2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
viewControllersArray = [[NSArray alloc] initWithObjects:controller1,controller2, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [[NSArray alloc] initWithArray:viewControllersArray];
for(int i = 0; i < tabBarController.tabBar.items.count; i++){
[[tabBarController.tabBar.items objectAtIndex:i] setTitle:[[tabBar.items objectAtIndex:i] title]];
}
isItemSelected = NO;
Also set the tag of tab bar items starting from 0. As 0 for first item, 1 for second item and so on.
and implement delegate method:
(void)tabBar:(UITabBar *)tabBar1 didSelectItem:(UITabBarItem *)item{
if(!isItemSelected){
isItemSelected = YES;
AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDel.window.rootViewController = tabBarController;
}
tabBarController.selectedIndex = item.tag;
tabBarController.selectedViewController = [viewControllersArray objectAtIndex:item.tag];
}
I think this is not the right way, as Concept of creating TabBar says that " Show the particular controller of related tab". If u do not select any tab than what else you will show on screen, "A black screen ?". so please re-architecture your design.

UITabBarControler inside Custom Container

i have been trying to put two controllers in the Container Controller.
First Controler
1) Just a View Controller with Few buttons
2) UITabBarController.
Below is the code snippet..
tabc = [[UITabBarController alloc] init];
hvc = [[HeaderViewController alloc] initWithNibName:#"HeaderViewController" bundle:nil];
[self addChildViewController:hvc];
[self.view addSubview:hvc.view];
[hvc didMoveToParentViewController:self];
bvc = [[BodyViewController alloc] initWithNibName:#"BodyViewController" bundle:nil];
bvc1 = [[Body1ViewController alloc] initWithNibName:#"Body1ViewController" bundle:nil];
UITabBar *tabBar = tabc.tabBar;
UITabBarItem *tabBarItem1 = [ tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [ tabBar.items objectAtIndex:1];
tabBarItem1.title = #"fruits";
tabBarItem2.title = #"vegs";
tabc.viewControllers = #[bvc,bvc1];
[self addChildViewController:tabc];
[self.view addSubview:tabc.view];
[tabc didMoveToParentViewController:self];
Problem:
In the tab view controller, only one tab is visible at a time [ i.e.: First tab ]
NOTE: My mistake, it shows both the tab, but the table name, or boundary of the tabs
are not visible. it shows as if the same tab. How to solve this issue ?
What is the quirk i need to be aware of ?.
It is a strange quirk that has bitten many. Tab bar controllers can only be the root controller and cannot be a child to the container. This is in apple's docs here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html
The section of interest is "The Views of a Tab Bar Controller"
The weird part is it can look like it works ok, but strange behaviors will crop up here and there.

navigating to ViewControllers that aren't on the UITabBar

I started using UITabBarController and it is great.
The thing is I have a few views that aren't accessed from the UITabBar
(either presented modal programatically or we want to have a button on the top to jump to them)
The thing is that I want to retain the Tab bar visible in these views.
From my understanding mixing presentViewController and UITabBarController is problematic.
How can I do that? Can I have "hidden" tab bar elements I can reference programatically?
Just to clarify with an example:
Views A,B,C,D are in the tab bar - via the storyboard - everything is peachy.
I NEED to have views E and F clickable from the top navigation (please don't suggest a sliding TabBar or a multiple line UITabBar).
I could just jump to E and F but I want the UITabBar to still be visible so the user can jump from E to A for example.
Just use the good old UINavigationController for every tab and just use [self.navigationController pushViewController:A animated:YES];
That's how the setup looks in code:
SGTabBarViewController *rootVC = [[SGTabBarViewController alloc] init];
SGFirstTabViewController *firstVC = [[SGFirstTabViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
SGSecondTabViewController *secondVC = [[SGSecondTabViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
SGThirdTabViewController *thirdVC = [[SGThirdTabViewController alloc] init];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdVC];
SGForuthTabViewController *fourhtVC = [[SGForuthTabViewController alloc] init];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:fourhtVC];
rootVC.viewControllers = #[navController1, navController2, navController3, navController4];
self.window.rootViewController = rootVC;
[self.window makeKeyAndVisible];
If you want your UITabBar visible on every VC you push just use hidesBottomBarWhenPushed = NO; on it.
However, there is no way to have UITabBar visible on views presented modally.

Adding a registration page before the tabViewController in IOS

I have a UITabBarController that is working good.
Now before I open the tabController, I want to check if the user is registered, if not I want to open a registration page (RegistrationController) and then go back to my tabView.
How can I do this, since the TabBarController can only be the first page.
Thank you
u can have a viewController(VC1) pushing another viewController (VC2) and in viewDidLoad of VC2 u can add ur tabbar programmatically.
tabBarController = [[UITabBarController alloc] init];
tabBarController.view.backgroundColor = [UIColor clearColor];
VC3 *object1 = [[VC3 alloc] initWithNibName:#"VC3" bundle:nil];
VC4 *object2 = [[VC4 alloc] initWithNibName:#"VC4" bundle:nil];
tabBarController.viewControllers = [NSArray arrayWithObjects:object1, object2, nil];
tabBarController.delegate = self;
[[tabBarController.viewControllers objectAtIndex:0] setTitle:#"title"];
[[tabBarController.viewControllers objectAtIndex:1] setTitle:#"title"];
[self.view addSubview:tabBarController.view];
[object1 release];
[object2 release];
also u can do this
in the viewDidLoad of the tabBarController (default view), u can check a variable, if its 0 (i.e. user is not registered), then u can presentModalViewController with a registration form.
hope it helps. happy coding :)
You can add an overlay, over the table view controller (another view which will make the table view not visible, since it will be behind the view you're adding). On succcessfull login / registration, you remove the overlay view.

Resources