UIButton over UIImageView

Discussion in 'iOS Programming' started by estupefactika, Jun 1, 2009.

  1. estupefactika macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #1
    Hi, I want to add a UIButton over a UIImageView but It looks dont recognize the button when I click it.

    Is there any way to do it without resorting to touches?? Thanks

    Code:
    contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    	[contentView setImage:[UIImage imageNamed:@"helloworld.png"]];
    
    	UIButton *b=[[UIButton alloc] init];
    	
    	b.center=CGPointMake(160, 240);
    
    	[b setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    	[b addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    	[contentView addSubview:b];
    	
    	// Provide support for auto-rotation and resizing
    	contentView.autoresizesSubviews = YES;
    	contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    
    	// Assign the view to the view controller
    	self.view = contentView;
        [contentView release]; // reduce retain count by one
    
     
  2. walty macrumors member

    Joined:
    Jul 15, 2008
    #2

    hi,

    I think u may need to do "sizeToFit" for the button and contentView..

    walty
     
  3. vocaro macrumors regular

    Joined:
    Mar 5, 2004
    #3
    Code:
    contentView.userInteractionEnabled = YES;
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    How about just using a UIButton (custom-style) and setting the image for it?
     
  5. vocaro macrumors regular

    Joined:
    Mar 5, 2004
    #5
    Because that's probably not what he wants. A typical scenario is a UIImageView acting as some sort of wallpaper background, with a smaller UIButton on top of it. However, if the button is a subview of the image view, as in the case of the OP, and the image view's user interaction mode is disabled (which it is by default), then the button will also have its user interaction disabled, hence the the problem.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Granted, that may be. Let's see if the OP comes back to clarify.
     
  7. braves4life macrumors newbie

    Joined:
    Oct 12, 2007
    #7
    UIImageView has userInteractionEnabled set to NO by default. This means none of its subviews will receive the event...
     
  8. peacetrain67 macrumors member

    Joined:
    Dec 20, 2007
    #8
    unless you are set on doing this programatically, you should be able to do this thru IB with a simple (somewhat dirty) workaround. In your document just don't place the button within the imageview but parallel to it... I've used this as a basic workaround and placing a clear png as the button's image to create an "illusion" of imageview interaction (which is possible, but not that necessary under most circumstances)...
     
  9. vocaro macrumors regular

    Joined:
    Mar 5, 2004
    #9
    With IB you don't need any workarounds. When you drag and drop a UIButton on top of a UIImageView, the button becomes a subview of the view, not the image view. And because the view's userInteractionEnabled property defaults to YES, the problem does not occur.
     

Share This Page