Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing a UIButton in the same space as UITabBar (When hidden)

I have a UITabBarController based application that I am developing. In one of my screen designs, I need to hide the tab bar (Which I do with UIView's setHidden method) I then placed a UIView where the tabBar would normally be and added a UIButton to the UIView. However when tapping the button I do not get any response. The button does not receive any touch events.

I've looked at some SO questions and answers for the solution. However most of them require subclassing. I've tried subclassing and overriding the HitTest event, however even that method is not called in my UIButton subclass.

I'm developing on iOS 7.1.

Can anyone shed some light on this and how to get the button to respond to touch events?

EDIT

My self and @Geet have looked at this issue a little more closely. It seems anything placed in the same space as the UITabBar does not receive touch events. As a test I created a new project with very little code that replicates my original projects setup.

In the sample app, we have one UIViewController embedded in a UITabBarController as the first tab. That UIViewController is then embedded in a UINavigationController. From there, we push a second UIViewController onto the stack.

Tapping the "Hide Tabbar" button at the top does just that. Hides the tabBar. Which exposes the green UIView that has been placed there in the storyboard, with a UIButton that is connected to the ViewControllers implementation file. This button does not receive any touch events at all. As soon as I move the button above the space where the TabBar would normally occupy, the button works. Move it back down towards the bottom of the screen - button no longer works.

It seems Apple has barred all touch events under the UITabBar. I wonder if there is a way around this?

I have tried:

self.tabbar.userInteractionENabled = NO;

Which did not help either.

Maybe there is a method that we are able to override the in a subclass of UITabbarController that will allow touches in this spec when the UITabBar is hidden?

If anyone wants to play around with the sample project - its here on my gitHub:

https://github.com/TanderZA/UIButton-in-UITabbar-Space

like image 865
Robert J. Clegg Avatar asked Jul 28 '14 07:07

Robert J. Clegg


Video Answer


3 Answers

Tander I finally figured it out, The button was not responding because its not receiving our touch events, you see In our storyboard, the View is not connected to the viewController, and due to this when I add the button programmatically to View It would never respond, A simple line solved this

In the function

- (IBAction)doneButton:(UIBarButtonItem *)sender

Just do this

- (IBAction)doneButton:(UIBarButtonItem *)sender
{
    /* Will hide or unhide the tabBar */

    self.tabBarController.tabBar.hidden=YES;
    [self.tabBarController setTabBarItem:nil];

    UIButton *butt=[UIButton buttonWithType:UIButtonTypeCustom];
    butt.backgroundColor=[UIColor redColor];
    butt.frame=CGRectMake(160, 530, 93, 40);
    [butt addTarget:self action:@selector(testButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarController.view addSubview:butt];



}

the self.tabBarController.view was our culprit, not letting the touch Events pas through, glad to have figured it out

like image 65
Geet Avatar answered Oct 21 '22 17:10

Geet


I recently encountered this myself, and in my case adding views to self.tabBarController.view was not an option.

Setting the tabBar as translucent (in addition to hiding it) allowed the touches to go through.

[self.tabBarController.tabBar setTranslucent:YES];
like image 45
Mark Pospesel Avatar answered Oct 21 '22 17:10

Mark Pospesel


I recently encountered this problem where we were hiding the UITabBar using isHidden but a UIView in one of the child view controllers underneath the UITabBar would not receive hit events. It received events everywhere but where the UITabBar used to be visible and located.

Rotating the phone back and forth would make the problem go away. The solution I eventually found is after setting isHidden to true you need to call setNeedsLayout on the UITabBarController view:

self.tabBarController.tabBar.isHidden = true;
self.tabBarController.view.setNeedsLayout();
like image 1
Werner Sharp Avatar answered Oct 21 '22 17:10

Werner Sharp