Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How access an uiview subview partially outside of his parent uiview?

I have the following UIVIew architecture (x,y,width,height) :

- MainWindow (0,0,768,1024)
    - MainView (0,0,768,80) 
        - containerview (500,40,120,80)
            - subview (500,40,120,80) 
                -some buttons

My problem is that the bottom of the subview lay outside of the bound of MainView. Buttons at the bottom of subview are not responsive. The one at the top are responsive, because their position is also inside on Mainview.

So when i try to click the buttons at the bottom of subview i actually click on MainWindow! Position of bottoms buttons of subview are not inside of MainView

Is there a way to make all my subview available even if half of it is outside of MainView bound?

I know that i can create the subview directly under MainWindow instead, but i don't want to redo my code.

Update Here how is design my views : A = MainWindow, B = MainView, C = container view, D = subview, X = where i want to click

+----------------------------+
|A                           |
|+-------------------------+ |
||B                        | |
||            +----------+ | |
|+------------|C&D       |-+ |
|             |X         |   |
|             +----------+   |
+----------------------------+

THank you

like image 334
peterphonic Avatar asked Dec 13 '22 09:12

peterphonic


1 Answers

You need to implement hitTest:(CGPoint)point withEvent:(UIEvent *)event in your MainView.

See the documentation

Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. Subviews may extend visually beyond the bounds of their parent if the parent view’s clipsToBounds property is set to NO. However, hit testing always ignores points outside of the parent view’s bounds.

Addressing a comment:

You should subclass UIView. Then set the class of MainView in the nib to your UIView subclass.

Some hittesting is discussed here, but I wasn't able to find clear explanation of how it works.

Try this StackOverflow question

I haven't tested it, but the following code might do what you need:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    for(UIView *subview in self.subviews)
    {
        UIView *view = [subview hitTest:[self convertPoint:point toView:subview] withEvent:event];
        if(view) return view;
    }
    return [super hitTest:point withEvent:event];
}
like image 170
Jiri Avatar answered Dec 26 '22 12:12

Jiri