I've got a rootView. Now I add a subview to my rootView by using the method addSubView:. After that the subview is added consisting of a view containing several buttons. Now what I want is, to be able to press both, the buttons on my rootView and the buttons on my subView. However when I turn off user interaction of my subView, I can't press its buttons any more. However if I let it on, I cant press the buttons of my rootView.
Can anyone help me?
For your "subview," subclass UIView and override the the hitTest method like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *subview = [super hitTest:point withEvent:event];
if ( subview != self )
return subview;
else
return nil;
}
This will cause the buttons and other views within your "subview" to respond to events, but the view itself will act as if it's not there.
Related
I was trying to implement a container view controller design into my app. However I was told that I need to support iOS 4.3 devices, so the official view controller API introduced in iOS 5 is not an option at the moment.
In order to achieve a similar behaviour I used a hack. Resized the view for my RootViewController and the added a subview to it that it's outside the view's bounds. For example: RootView has bounds 0,0,320,480. Now I resized it to 0,0,320,430 and included a subview at 0,430,320,60. This works since I do all calculations using the ApplicationFrame giving me stable frames on which to work. But the problem I'm facing right now is that the subview which is out of the bounds of the view is not receiving touch events. The maskToBounds = NO property helps me with the display. But touches? Anybody know how to do that?
Whenever you want the subview to receive touch events in such cases, you can do the following:
1- Create a new class that inherits from UIView and override hitTest:withEvent: to allow subviews to intercept touches:
#interface CustomView : UIView
#end
#implementation CustomView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
/// Check if the point is inside the subview
CGPoint newPoint = [subview convertPoint:point fromView:self];
if ([subview pointInside:newPoint withEvent:event]) {
/// Let the subview decide the return value
return [subview hitTest:newPoint withEvent:event];
}
/// Default route
return [super hitTest:point withEvent:event];
}
#end
2- Change the class of root view to our CustomView (from the right panel in Xcode > Identity Inspector > Custom Class).
And we're done!
I'm creating a custom dropdown menu inside a cell of a UITableView adding this menu as a subview of the cell content view.
When I click on the menu inside the cell I enlarge the view to show all the menu items: now the menu view is over the tableView and all cells.
So, when I select on of the items inside my custom view, the TouchesBegan it's triggered only in the part of the view that's over the UITableView cell in which I call the menu and not in all the view that I enlarged.
If I touch the view in part that is over another cell, the TouchesBegan it's triggered in the cell of the table view and not in the view on top of all cells.
Any suggestion?
Views typically don't respond to touch events on views or portions of views drawn outside its bounds. There are a few possible options to fix the issue:
Enlarge the cell when the dropdown menu is visible to fit the entire menu, but this might negatively impact the visual effect depending on your design.
Add the menu on the controller instead of the cell view. If you got this route, you can use the UIScrollViewDelegate events from the table view to reposition the menu as the user scrolls the table. Remember to account for the case where the menu is displayed on the last cell and might overdraw the controller's bounds if always drawn in downward direction.
Override hitTest:withEvent: and pointInside:withEvent on both your menu view and its superview to inform the OS that you respond to events even outside its normal bounds.
The following worked for me:
Subclass your cell and connect the root view of your layout with the cell:
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIView *rootView;
#end
Override hitTest of the cell like this:
#implementation CustomCell
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
BOOL res = [self.rootView pointInside:point withEvent:event];
if(res){
return [self.rootView hitTest:point withEvent:event];
}
return [super hitTest:point withEvent:event];
}
#end
Now touchesBegin/Move/End/Cancel will be delivered to the view and it's hierarchy whenever touch is inside that view
I have a UIView which has a bunch of subviews. The subviews should be able to receive touch events, but for some reason the parent UIView takes the touch and does not pass it on. I have created it in a very standard way like I always create views:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1024,768)];
self.mainView = myView;
[myView release];
[self.view addSubview:self.mainView];
Then I create subviews and add them as normal:
[self.mainView addSubview:someOtherView];
I know self.mainView is getting the touch events when I listen in the main UIWindow:
VIEW: <UIView: 0x8d34aa0; frame = (0 0; 1024 768);
But why in the world can I not get the subviews to receive touches? I don't understand why this happens sometimes. I am not changing any default properties of mainView.
Had to do with the frame of the parent view. If the frame doesn't enclose the subviews they won't receive touches.
UIView touches do not get passed to subviews if they lie outside of the superview's bounds as mentioned by the solution.
However if you want these subviews to respond to touch, override hitTest:withEvent: of the superview.
Documentation on Event Delivery
Touch events. The window object uses hit-testing and the responder chain to find the view to receive the touch event. In hit-testing, a window calls hitTest:withEvent: on the top-most view of the view hierarchy; this method proceeds by recursively calling pointInside:withEvent: on each view in the view hierarchy that returns YES, proceeding down the hierarchy until it finds the subview within whose bounds the touch took place. That view becomes the hit-test view.
Create a subclass of UIView (or other UIView subclass).
Override hitTest:withEvent.
Use this UIView subclass as the superview, so subview can respond to touches.
Add method below in subclass:
(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
NSEnumerator *reverseE = [self.subviews reverseObjectEnumerator];
UIView *iSubView;
while ((iSubView = [reverseE nextObject])) {
UIView *viewWasHit = [iSubView hitTest:[self convertPoint:point toView:iSubView] withEvent:event];
if(viewWasHit) {
return viewWasHit;
}
}
return [super hitTest:point withEvent:event];
}
Note: Reverse enumerator used since subviews are ordered from back to front and we want to test the front most view first.
Does you touch handlers call the superclass handlers?
e.g. in your touchesBegan, calling:
[super touchesBegan:touches withEvent:event];
In short, I want to detect a touch on the navigation controller titlebar, but having trouble actually catching any touches at all!
Everything is done without IB, if that makes a difference.
My app delegate's .m file contains:
MyViewController *viewController = [[MyViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navigationController.view];
There are a few other subviews added to this window in a way that overlays navigationController leaving only the navigation bar visible.
MyViewController is a subclass of UIViewController and its .m file contains:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
NSLog(#"ended\n");
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
NSLog(#"began\n");
}
}
I also tried putting these functions directly into app delegate's .m file, but the console remains blank.
What am I doing wrong?
Well, for lack of a better idea, I added another subview to my app, clear in color, placed programmatically over the navigation bar title and used a custom class for that view with relevant touch methods overridden. Works fine, but the I still wish there was a more elegant solution.
The view controller is inserted into the responder chain between its managed view and the superview:
Because view controllers are tightly bound to the views they manage, they are also part of the responder chain used to handle events. View controllers are themselves descendants of the UIResponder class and are inserted into the responder chain between the managed view and its superview. Thus, if the view managed by a view controller does not handle an event, it passes the event to its view controller, which then has the option of handling the event or forwarding it to the view’s superview.
(the UIViewController documentation)
Is it possible, that the managed view of your controller is eating all the events? What kind of view is it?
Had trouble with this, as my custom view was deeper in the view hierarchy. Instead, I climbed the responder chain until it finds a UIViewController;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Pass to top of chain
UIResponder *responder = self;
while (responder.nextResponder != nil){
responder = responder.nextResponder;
if ([responder isKindOfClass:[UIViewController class]]) {
// Got ViewController
break;
}
}
[responder touchesBegan:touches withEvent:event];
}
try adding the method userInteractionEnabled = YES to your UIImageView
These methods should be put into the UIView subclass not the UIViewControllers...The UIView will receive the touches call backs, then you can make a protocol on the UIView and implement it on the UIViewController so the UIViewController will receive some call back when the touch events occur...Here is a link that talks about protocols and how to define them and implement them Protocols Ref
I have a custom UITableViewCell that has a UIScrollView in it that covers the entire cell. I want to be able to use the scroll view to scroll a label in the cell (which is working) but the scroll view seems to be 'stealing' the cell's tap event. So I was wondering:
How do you pass a touch event from a UIScrollView to its parent UITableViewCell?
In the UISCrollViewController, have you tried passing the touchesEnded on to super, assuming a scroll is not in progress?
- (void)touchesEnded:(NSSet *)aTouches withEvent:(UIEvent *)anEvent
{
if(![self isDragging])
{
[super touchesEnded:aTouches withEvent:anEvent];
}
}