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
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
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];
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With