0

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

galactikuh
  • 765
  • 8
  • 18

1 Answers1

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Could you please explain why it is outside the bounds? The superview is the view managed by a UICollectionViewController that contains all the beige "buttons" with plus signs and the toolbar at the bottom (with labels). – galactikuh Aug 19 '17 at 22:27
  • It's also worth noting that I used: UIView *lastView=[self.view.subviews lastObject]; To check whether the expected view is on top of my superview, and indeed, it is. – galactikuh Aug 19 '17 at 22:36
  • Updated my Question with bounds. – galactikuh Aug 19 '17 at 22:45
  • The question is: what is the bounds and frame of the LinkButtonView, and what is the bounds and frame of its superview. – matt Aug 19 '17 at 23:01
  • Right, that is what I provided. – galactikuh Aug 19 '17 at 23:36
  • Ok, but this needs to happen all the way down. What about the LinkButtonView's subview, the toolbar? – matt Aug 19 '17 at 23:50
  • toolbar (within LinkButtonView) bounds : 237.000000,1.000000,184.000000,56.000000. First bar button (within toolbar) bounds 0.000000,0.000000,44.000000,56.000000. That x location on the toolbar is curious... – galactikuh Aug 20 '17 at 10:16
  • Ooooohh... I know what I'm doing wrong.. I am using the original button's frame to initialize the toolbar frame. I should be using it to initialize the popup view's frame. CGRect frame = CGRectMake(rect.origin.x-12, rect.origin.y, rect.size.width*4, TOOLBARH); Thanks for your help! – galactikuh Aug 20 '17 at 13:25
  • 1
    "I am using the original button's frame to initialize the toolbar frame" Yes, that's what I suspected. By the way, using the View Debugger would have gotten you to this point a lot faster, because you can _see_ the views in relation to one another. — Anyway, the moral is clear: when a tap falls through, always suspect view-outside-superview. – matt Aug 20 '17 at 14:27
  • I didn't know about the visual debugger. I am including this for others who don't either.. https://developer.apple.com/videos/play/wwdc2016/410/ – galactikuh Aug 21 '17 at 12:38