Calling an ActionSheet from a TabBar

I have a tab bar with 4 buttons ("Home", "Visits", "Share", "More"). I want the Share button on my tab bar to call up an action sheet that has corresponding buttons for selecting the channels that I want the user to be able to distribute links to the application (Facebook, Twitter, email, etc.).

I can do this using the Storyboard, but I need to create a unique (empty) view controller that is connected to the Share button on the tab bar. I put the following code in the viewDidAppear method to display the action sheet when the Share button is selected:

int selectedTab = self.tabBarController.selectedIndex; if (selectedTab == 2) { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share Your Visit with Friends!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter", @"Email eduLaunchpad", nil]; [actionSheet showFromTabBar:self.tabBarController.tabBar]; } 

When I close the worksheet, I return to the default home view:

 [self.tabBarController setSelectedIndex:0]; 

While this works, it is not optimal from the point of view of the user. I would prefer the action sheet to appear above the specific view that was shown when the Share button was selected. For example, if the user was in the Visits view and selected Share on the tab bar, I would like the action sheet to appear at the top of the visits window, and this view would be visible behind the action sheet.

Is it possible to achieve this with a storyboard? Or do I need a custom tab bar to implement this feature? If a custom tab bar is required, I would like some guidance / insight on how to implement this, including a custom tab bar, as well as an action table from the tab bar.

Thanks in advance!

+4
source share
2 answers

I also needed this, googled around, and eventually built the following category in the UITabBarController.

The trick to making the action sheet appear on the current tab is that you never want to visit this presentation tab. Cover the tab bar at this position with the UIButton that initiates the action sheet.

Draw the tab bar, as usual, in the storyboard, leaving an empty element in the position where the special behavior should occur. Then add this category ...

 // UITabBarController+Button.h @interface UITabBarController (Button) <UIActionSheetDelegate> - (void)replaceItemAtIndex:(NSUInteger)index withButtonImage:(UIImage*)buttonImage; @end // UITabBarController+Button.m #import "UITabBarController+Button.h" #define kBUTTON_TAG 4096 @implementation UITabBarController (Button) - (void)replaceItemAtIndex:(NSUInteger)index withButtonImage:(UIImage*)buttonImage { UIButton *button = (UIButton *)[self.view viewWithTag:kBUTTON_TAG]; if (!button) { button = [UIButton buttonWithType:UIButtonTypeCustom]; button.tag = kBUTTON_TAG; [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; UITabBar *bar = self.tabBar; CGFloat width = bar.frame.size.width / bar.items.count; button.frame = CGRectMake(index*width, bar.frame.origin.y, width, bar.frame.size.height); [self.view addSubview:button]; } } #pragma mark - Handle button tap - (void)buttonTapped:(id)sender { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Action Sheet:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Action A", @"Action B", @"Action C", nil]; [sheet showFromTabBar:self.tabBar]; } 

Name it the index <tabBar.items.count and the image for the button. If this is your root vc application, you can call it like this:

 #import "the category i suggest above" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // whatever else you do in here, then NSLog(@"%@", self.window.rootViewController); // make sure this is a UITabBarController // if it is, then ... UITabBarController *tbc = (UITabBarController *)self.window.rootViewController; [tbc replaceItemAtIndex:2 withButtonImage:[UIImage imageNamed:@"some image name"]]; return YES; } 
+1
source

Instead of opening the action table from the tab

 [actionSheet showFromTabBar:self.tabBarController.tabBar]; 

use this code.

 [actionSheet showInView:[UIApplication sharedApplication].keyWindow]; 
0
source

All Articles