KVISH
KVISH

Reputation: 13178

UIScrollView with multiple subViews

I have a UIScrollView with many other subviews inside it. Most of the subviews are UITextView's and when they are all loaded, the scrolling and everything is fine. But for one of the views, I am loading a UIView with a MKMapView and a UITextView inside of it. When the user wants to scroll the UIScrollView, they cannot touch the UIView or its contents. I cannot set setUserInteractionEnabled to NO because I need the user to be able to click on the MKMapView and then go to another UIViewController for the map. Are there any suggestions regarding this? I have my code for the above below:

    CGRect dealDescRect = CGRectMake(10, 10, delegate.scrollView.frame.size.width - 22 - 20, 120);
    mapView = [[MKMapView alloc] initWithFrame:dealDescRect];
    mapView.layer.cornerRadius = cornerRadius;
    mapView.scrollEnabled = NO;
    mapView.zoomEnabled = NO;

    BOOL result = [self loadAddressIntoMap];
    if (result == TRUE) {
        UITapGestureRecognizer* recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [mapView addGestureRecognizer:recognizer];
    }

    UITextView *addressTextView = [self generateTextView:addressText :5];
    addressTextView.editable = NO;
    [addressTextView setFont:[UIFont systemFontOfSize:fontSize]];
    [addressTextView setUserInteractionEnabled:NO];
    CGRect addressTextViewFrame = addressTextView.frame;
    addressTextViewFrame.origin.x = 0;
    addressTextViewFrame.origin.y = 130;
    addressTextViewFrame.size.height = addressTextView.contentSize.height + 15;
    addressTextView.frame = addressTextViewFrame;

    CGRect viewRect = CGRectMake(10, 145, delegate.scrollView.frame.size.width - 22, addressTextView.contentSize.height + 135);
    viewRect.origin.x = 11;
    viewRect.origin.y = startTop;
    UIView *view = [[UIView alloc] initWithFrame:viewRect];
    view.layer.cornerRadius = cornerRadius;

    [view setBackgroundColor:[UIColor whiteColor]];
    [view addSubview:mapView];
    [view addSubview:addressTextView];

EDIT

For some weird reason, if I change the UIView to a UITextView, it works! Not sure what the real solution here is though. I just disable editing.

Upvotes: 0

Views: 790

Answers (1)

Josh Hudnall
Josh Hudnall

Reputation: 1031

If it were me, instead of using gesture recognizers to watch for a tap on the map I'd create a UIButton of a custom type (UIButtonTypeCustom) and give it no background and no text, and place it on top of the map with the same frame as the map.

This has the benefit of preventing the user from interacting with the map, moving to the next page as you want and if the user starts scrolling even when over the map they are able to.

Upvotes: 1

Related Questions