View Full Version : UIButton over UIImageView
estupefactika
Jun 1, 2009, 11:50 AM
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
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
walty
Jun 8, 2009, 02:29 AM
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
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
hi,
I think u may need to do "sizeToFit" for the button and contentView..
walty
vocaro
Sep 1, 2009, 12:44 PM
contentView.userInteractionEnabled = YES;
dejo
Sep 1, 2009, 01:22 PM
How about just using a UIButton (custom-style) and setting the image for it?
vocaro
Sep 1, 2009, 07:52 PM
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.
dejo
Sep 1, 2009, 11:24 PM
Because that's probably not what he wants.
Granted, that may be. Let's see if the OP comes back to clarify.
braves4life
Sep 1, 2009, 11:36 PM
UIImageView has userInteractionEnabled set to NO by default. This means none of its subviews will receive the event...
peacetrain67
Sep 2, 2009, 01:24 AM
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
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
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)...
vocaro
Sep 2, 2009, 02:19 AM
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.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.