I have a UITabViewController. I'm trying to modally present a UIImagePickerController when a specific tab bar item is selected. It must appear over the current UIViewController. This is like what happens in the Instagram or Periscope app when you press on the camera icon. However I can't figure out how to display the UIImagePickerController without displaying the UIViewController that's connected to the Tab Bar Item. Is this possible?
Thanks,
Yusuf
You can use the shouldSelectViewController method of UITabBarControllerDelegate to accomplish something like this.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
// Check if it's the "image picker" tab
if (viewController == self.imagePickerTabView) {
// Present image picker
return NO;
}
// All other cases switch to the tab
return YES;
}
Related
I want to click tab bar to pop up a modal view not just view controller.
(Like Instagram's camera tab bar to pup up camera view)
However when I drag from navigation controller to another view controller
I always got a black view when I run the app if I choose present modally.
So now, I can only choose root view controller.
Sorry for my poor English !
Can anyone understand and help me ?
The easiest way is to subclass uitabbarcontroller and use its delegate:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[SomeVC class]]) {
[self.navigationController presentViewController:vcToPresentModal animated:YES completion:NULL];
return NO;
}
return YES;
}
The main idea is to catch selection of some vc(it could be just empty UIViewController, connected with item, where should be modal presentation), cancel it,and show a modal vc.
I have a UITabBarController with five tab bar items, one of them is share button action (to share the itunes store link - tell a friend). My issue is when I click on this tab, the UIActivityViewController has displayed with blank controller, I need to show the share window (UIActivityViewController) to appear above the last tab bar view selected to avoid the blank view of this tab bar item.
You can implement the UITabBarControllerDelegate protocol and use the
- tabBarController:shouldSelectViewController: method to intercept the tap on the share tab like this:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (viewController == [tabBarController.viewControllers objectAtIndex:/*share button index*/]){
//show UIActivityViewController
return NO;
}
return YES;
}
I have created a tab bar view controller with 3 bar item, in storyboard.
Now on tap of a tab bar item I want to present a ViewController which is connected to tab bar item via Navigation controller.
How this can be achieved programmatically since I have not created any tab bar object.
(OR)
Is there a way to capture the tab bar item selection (which is created in storyboard)
Thank You...
Add this in viewDidLoad: to assign view controllers to each of the buttons:
self.tabBarController.viewControllers =
#[firstViewController,secondViewController,thirdViewController];
Here is how to detect the tap of the button:
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 0) {
// present your first view controller
}
else if (tabBarController.selectedIndex == 1) {
// present your second view controller
}
else if (tabBarController.selectedIndex == 2) {
// present your third view controller
}
}
Use the UITabBarControllerDelegate to get notified about which tab got selected.
I'm working on an app and I would like to use the tab bar controller styling, but without the tabs. I would like it so that when I click on a tab button a function is run, but I don't want it to jump to a new tab. Is this possible and if so how do I do it?
Yes it is possible.
Implement the UITabBarControllerDelegate protocol's tabBarController:shouldSelectViewController: method execute your code and return NO.
Example
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
// Do your stuff here.
return NO;
}
I have a UITabBarController in the root of my storyboard with three tabs. One of the tab is used to display the user information. I have set this tab to transition to the register/login page if the user has not registered. But the ViewController gets popped(and corrupts the Navigation Bar) when I double tap the tab icon. I cannot use the modal transition as it hides the TabBarController. What is the best solution to solve this issue?
You can use 'shouldSelectViewController' delegate of TabViewConroller to check if the selected viewController is same as the clicked one and skip it accordingly.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if(viewController == tabBarController.selectedViewController)
{
return NO;
}
}