Take the 2-minute tour ×

Let's say if 2 small UIViews has been added to the view. Then 3rd UIView would be added on top, How do I determine if 3rd UIView would overlapped the first 2 UIViews?

share|improve this question
Compare the frames. –  aroth Oct 6 '12 at 0:42
CGRectIntersectRect BOOL –  0x7fffffff Oct 6 '12 at 0:44
How about how many sub UIViews underneath it would overlapped? Any helper function that would return an integer indicates how many UIViews it would overlapped? –  jason white Oct 6 '12 at 0:51
There isn't one. You'll have to cycle through the subviews and increment a count yourself every time one is detected. –  0x7fffffff Oct 6 '12 at 0:53
add comment

1 Answer

You could write a method on your UIView subclass (the container) that calculated this for you pretty easily:

-(NSArray*)getOverlappingViewsForSubview:(UIView*)mySubview {
    NSMutableArray* overlappedSubviews = [[[NSMutableArray alloc] init] autorelease];

    for(UIView* someSubview in self.subviews) {
        if(CGRectIntersectsRect(mySubview.frame, someSubview.frame)) {
            [overlappedSubviews addObject:someSubview];

    return overlappedSubviews;

Something like that should work -- you can call count on the result of that method if you only need to know how many views intersected (or rewrite the method to return a count instead of an array of the intersected views).

share|improve this answer
add comment

Your Answer


