Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

estupefactika

macrumors member
Original poster
Feb 16, 2009
47
0
Alcobendas (Madrid)
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
 

walty

macrumors member
Jul 15, 2008
33
0
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


hi,

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

walty
 

vocaro

macrumors regular
Mar 5, 2004
120
0
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.
 

braves4life

macrumors newbie
Oct 12, 2007
11
0
UIImageView has userInteractionEnabled set to NO by default. This means none of its subviews will receive the event...
 

peacetrain67

macrumors member
Dec 20, 2007
68
0
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

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

macrumors regular
Mar 5, 2004
120
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.