2010-08-20 8 views
19

chicos, quiero hacer clic en mi uiButton después de que se haya agregado a UIImageVIew, pero no funciona. este es mi código:no se puede hacer clic en UIButton cuando se agrega a UIImageView

UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain]; 

btnDetail.frame = CGRectMake(0,0,100,100); 
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents]; 

[self.imageView addSubview:btnDetail]; 

el botón no puede hacer clic cuando lo añado a UIImageView, pero si no lo agregue a UIImageView, funciona correctamente. favor que alguien me ayude

Respuesta

47

Nota: Por defectoUserInteraction la propiedad de UIImageView se establece en NO. Este es el lugar donde la mayoría de nosotros comete errores. Por lo tanto, lo más importante para registrar mientras agrega cualquier control a UIImageView es establecer su propiedad UserInteractionEnabled en .

[self.imageView setUserInteractionEnabled:YES]; 

Así que modificar el código de la siguiente manera:

UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain]; 

btnDetail.frame = CGRectMake(0,0,100,100); 
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents]; 

[self.imageView addSubview:btnDetail]; 
[self.imageView bringSubviewToFront:btnDetail]; 
[self.imageView setUserInteractionEnabled:YES]; 

Codificación felices ...

+0

He intentado su código, pero no pasa nada, todavía no puedo hacer clic en él. para obtener más información: antes de agregar el UIButton, he agregado otro UIImage a esta UiVIewImage, ¿hay algún efecto? –

+0

Por favor revisa el nuevo código ... Lo he implementado y probado. Está funcionando bien ahora ... En realidad, la interacción del usuario de la vista de la imagen está desactivada por defecto, por lo que no podrá hacer clic en cualquier elemento que se le agregue. Así que he configurado su interacción de usre como habilitada. Ahora funciona bien –

+1

aunque funciona a la perfección, no puedo creer que haya cometido un error [self.imageView setUserInteractionEnabled: YES]; antes de agregar la subvista, seguro que no funcionará, hha ridiclous ,, por cierto, muchachos, muchachos ... –

5

UIImageView tiene userInteractionProperty conjunto de NO por defecto por lo que no se ocupa de toques y no se propaga a sus subvistas. Intente configurarlo en SÍ:

self.imageView.userInteractionEnabled = YES; 
+0

lo he intentado, pero todavía no funciona –

+0

Lo he intentado en un proyecto de muestra. Funciona perfectamente para mí ... – Vladimir

+0

, chicos, funciona perfectamente para mí también ... –

3

hey tuve este código. su trabajo adecuadamente

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button.frame = CGRectMake(80.0, 210.0, 100.0, 20.0); 
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside]; 
[button setTitle:@"ShowView" forState:UIControlStateNormal]; 

[image2 addSubview:button]; 
[image2 bringSubviewToFront:button]; 
[image2 setUserInteractionEnabled:YES]; 
0

Si alguien tiene un problema con la interacción UIButton en UIImageView hacer los siguientes pasos.

1 Crear propiedad de UIImageView:

@property (nonatomic, strong) UIImageView *buttonImageView; 

2 Que sintetizarlo:

@synthesize buttonImageView = _buttonImageView; 

3 Crear método de aplicación:

- (UIImageView *) buttonImageView { 

    _buttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
    [_buttonImageView setBackgroundColor:[UIColor whiteColor]]; 
    [_buttonImageView setUserInteractionEnabled:YES]; 

    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [addButton setTitle:@"add button" forState:UIControlStateNormal]; 
    addButton.frame = CGRectMake(0, 0, 160, 50); 
    [addButton setUserInteractionEnabled:YES]; 
    [addButton setEnabled:YES]; 
    [addButton setAlpha:1]; 
    [addButton addTarget:self action:@selector(addNewImage) forControlEvents:UIControlEventTouchUpInside]; 

    return _buttonImageView; 
} 

4 Por ejemplo, en el método viewDidLoad añadir subvista a su punto de vista:

[self.view addSubview:self.buttonImageView]; 
Cuestiones relacionadas