Search code examples
iosobjective-cuiviewactionuibuttonbaritem

iOS UIBarButtonItem action falls through to button behind it


I am creating a custom UIView to act as a "pop up" window with a toolbar. The action for each UIBarButtonItem in the toolbar is triggered as expected on iPhone simulator but is not triggered on iPad or iPad simulator. Instead, the action for the button behind the pop up view is triggered. I am targeting iOS 8. What might be the problem?

This is where the custom view is being created. This is inside a subclass of UIView. It contains the selectors:

-(void)setupButtons: (SandBoxViewController*)ctrl :(UIImage*)img1 :(UIImage*)img2 :(UIImage*)img3 :(CGRect) rect
{

    controller=ctrl;
    CGRect frame = CGRectMake(rect.origin.x-12, rect.origin.y, rect.size.width*4, TOOLBARH);
    toolbar = [[UIToolbar alloc]initWithFrame:frame];


    [toolbar setBarStyle:UIBarStyleBlackTranslucent];

    UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc]
                                    initWithImage:img1 style:UIBarButtonItemStylePlain
                                    target:self action:@selector(setToOne:)];

    UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc]
                                    initWithImage:img2 style:UIBarButtonItemStylePlain
                                    target:self action:@selector(setToTwo:)];

    UIBarButtonItem *customItem3 = [[UIBarButtonItem alloc]
                                    initWithImage:img3 style:UIBarButtonItemStylePlain
                                    target:self action:@selector(setToThree:)];


    NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem1, customItem2,customItem3,nil];
    [toolbar setItems:toolbarItems];
    [self addSubview:toolbar];
}

This creates the custom view and calls the above function. It is in the ViewController:

-(void)setupLongPressButtonView: (CGRect)frame
{
    self.buttonView =[[LinkButtonView alloc]initWithFrame:frame];

    UIImage *image1=[[UIImage imageNamed:@"connect1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *image2=[[UIImage imageNamed:@"connect2.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *image3=[[UIImage imageNamed:@"connect3.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];


    //retrieves the button that I want the popup to be placed above
    UIBarButtonItem *button=[self.toolbarItems objectAtIndex:self.linkButtonIndex];
        CGRect buttonFrame=button.customView.frame;
        [self.buttonView setupButtons:self :image1 :image2 :image3 :buttonFrame];
        [self.view addSubview:self.buttonView];
CGRect superv=self.view.frame;
CGRect subv=self.buttonView.frame;
NSLog(@"superview bounds: %f,%f,%f,%f. subview bounds: %f,%f,%f,%f",superv.origin.x,superv.origin.y,superv.size.width,superv.size.height,subv.origin.x,subv.origin.y,subv.size.width,subv.size.height);


    }

Clicking with long press on the button marked Link causes the view to pop up above it. See that the pop up view buttons show up OK but clicking on them causes the button's action behind them to be triggered (as if its ignoring the view).

UIView

Note: I also tried to create a custom view with a UIButton for the UIBarButtonItem but it didn't make any difference.

Update: Here are the bounds of the superview and the view containing the buttons in question: superview bounds: 0.000000,0.000000,768.000000,1024.000000 subview bounds: 0.000000,912.000000,224.000000,56.000000 So the sub is within the constraints of the super


Solution

  • as if its ignoring the view

    Exactly right. It is ignoring it. The pop-up view is outside the bounds of its superview. A view outside the bounds of its superview is, by default, untouchable. Taps on it just fall through as if it weren't there. This is totally normal, expected behavior.