2010-05-04 9 views
5

¿Hay alguna manera de cambiar el marco de UIAlertView en iPhone o iPad? Intenté cambiar el marco en el siguiente método de delegado - (void)willPresentAlertView:(UIAlertView *)alertView;cambie el ancho de UIAlertView en el iPad

pero incluso así el ancho de la vista de alerta permaneció sin cambios. Y creo que un pequeño Alertview en iPad no tendrá sentido. Y creo que debe haber una manera de lograrlo, al menos en el iPad.

Gracias, krishnan.

+0

¿Alguna vez encontrar una manera de hacer esto? Solo curiosidad, muchas respuestas aquí, hace tres años ... etc. – Morkrom

Respuesta

8

Hmm, este código funciona bien (excepto, cuando se está tratando de disminuir el tamaño - causa, en este caso se puede conseguir un poco de mala prestación):

- (void)willPresentAlertView:(UIAlertView *)alertView { 
    alertView.frame = CGRectMake(x, y, width, height); 
} 

Tal vez, se le olvidó preparar UIAlertView 's delegado con algo como someAlertView.delegate = self;

 

ACTUALIZACIÓN   ↓

codepart:

- (IBAction)go:(id)sender { 
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:nil 
     otherButtonTitles:nil] autorelease]; 
    alert.delegate = self; 
    [alert show]; 
} 

- (void)willPresentAlertView:(UIAlertView *)alertView { 
    alertView.frame = CGRectMake(5.f, 1.f, 100.f, 200.f); 
} 

Resultado (en mi iPod): http://dl.dropbox.com/u/6455784/alert_view_frame.png

+1

Establecí el delegado. La altura de AlertView cambió pero no el ancho. ¿Realmente funcionó para ti? – Krishnan

+0

Sí. Pero, cuando el ancho de la vista era menor que el necesario para mostrar el texto correctamente (renderizado), el marco de fondo de la vista se procesaba mal (la parte izquierda del rectángulo se redondeaba, pero la derecha no). – kpower

+0

¿Puedes confirmar que tu solución funciona? astalapista dice que no está funcionando. – Krishnan

3

No hay campos de texto están presentes en mi alertview, aún cuando fijo marco o uso transformar de la vista fotograma de fondo fue dictada mal en el dispositivo . Esto funcionará bien solo en el simulador.

3

Sé que esta es una vieja pregunta, pero estaba enfrentando el mismo problema y las respuestas aquí me ayudaron. Necesitaba agrandar los campos de título y texto del mensaje para acomodar más texto en el iPad.

Lo que hice fue implementar el método delegado UIAlertView willPresentAlertView: y agregar dos objetos UILabel para servir como título y mensaje. Fui capaz de configurar el tamaño del marco y el origen de estas dos etiquetas. Después de obtener el tamaño deseado para los campos de texto, cambio el tamaño de la vista de alerta para acomodar el nuevo texto. Luego repito las subvistas de la vista de alertas para encontrar el botón y ajustar su marco.

- (void)willPresentAlertView:(UIAlertView *)alertView { 

     // add a new label and configure it to replace the title 
     UILabel *tempTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,20,350, 20)]; 
     tempTitle.backgroundColor = [UIColor clearColor]; 
     tempTitle.textColor = [UIColor whiteColor]; 
     tempTitle.textAlignment = UITextAlignmentCenter; 
     tempTitle.numberOfLines = 1; 
     tempTitle.font = [UIFont boldSystemFontOfSize:18]; 
     tempTitle.text = alertView.title; 
     alertView.title = @""; 
     [alertView addSubview:tempTitle]; 
     [tempTitle release]; 

     // add another label to use as message 
     UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 350, 200)]; 
     tempLabel.backgroundColor = [UIColor clearColor]; 
     tempLabel.textColor = [UIColor whiteColor]; 
     tempLabel.textAlignment = UITextAlignmentCenter; 
     tempLabel.numberOfLines = 20; 
     tempLabel.text = alertView.message; 
     [alertView addSubview:tempLabel]; 

     // method used to resize the height of a label depending on the text height 
     [self setUILabel:tempLabel withMaxFrame:CGRectMake(10,50, 350, 300) withText:alertView.message]; 
     alertView.message = @""; 

     // set the frame of the alert view and center it 
     alertView.frame = CGRectMake(CGRectGetMinX(alertView.frame) - (370 - CGRectGetWidth(alertView.frame))/2 , 
            alertView.frame.origin.y, 
            370, 
            tempLabel.frame.size.height + 120); 


     // iterate through the subviews in order to find the button and resize it 
     for(UIView *view in alertView.subviews) 
     { 
      if([[view class] isSubclassOfClass:[UIControl class]]) 
      { 
       view.frame = CGRectMake (view.frame.origin.x+2, 
              tempLabel.frame.origin.y + tempLabel.frame.size.height + 10, 
              370 - view.frame.origin.x *2-4, 
              view.frame.size.height); 
      } 
     } 

     [tempLabel release]; 
} 

y el método utilizado para cambiar el tamaño de la etiqueta:

- (void)setUILabel:(UILabel *)myLabel withMaxFrame:(CGRect)maxFrame withText:(NSString *)theText{ 
    CGSize stringSize = [theText sizeWithFont:myLabel.font constrainedToSize:maxFrame.size lineBreakMode:myLabel.lineBreakMode]; 
    myLabel.frame = CGRectMake(myLabel.frame.origin.x, 
           myLabel.frame.origin.y, 
           myLabel.frame.size.width, 
           stringSize.height 
           ); 
} 

No sé si esta es la mejor manera de hacer esto, pero funcionó para mí.

+0

Sí, estaba trabajando antes de ios7, pero para iOS 7 no funciona. – UserDev

4

esta es la solución que buscas si simplemente deseas cambiar el ancho del UIAlertView estándar con un campo de texto grande y un botón Aceptar. En mi caso, solo quería una alerta con una gran cantidad de texto. Y tenga ese texto todo visible sin tener que desplazarse por la vista de texto. El truco es que tienes que cambiar el ancho de todas/algunas de las vistas dentro. Sin garantías, esto aprobará la aprobación de la aplicación. Podrían encontrar UIAlertTextView y rechazarlo.

#define ALERT_VIEW_WIDTH 600 
#define ALERT_PADDING 20 
-(void) willPresentAlertView:(UIAlertView*) alertView 
{ 
    id thing; 
    int center=alertView.center.x-ALERT_VIEW_WIDTH/2; 
    for (thing in alertView.subviews) 
    { 
     NSLog(@"%@", [[thing class] description]); 
     if([[[thing class] description] isEqualToString:@"UIAlertTextView"]) 
     { 
      UIView* v=(UIView*)thing; 
      v.frame=CGRectMake(v.frame.origin.x+ALERT_PADDING, v.frame.origin.y, ALERT_VIEW_WIDTH-ALERT_PADDING*3, 250); 
     } 
     if([[[thing class] description] isEqualToString:@"UIThreePartButton"]) 
     { 
      UIView* v=(UIView*)thing; 
      v.frame=CGRectMake(v.frame.origin.x+ALERT_PADDING, v.frame.origin.y, ALERT_VIEW_WIDTH-ALERT_PADDING*3, v.frame.size.height); 
     } 
    } 
    alertView.frame=CGRectMake(center, alertView.frame.origin.y, ALERT_VIEW_WIDTH, 360); 

} 
+0

¿Obtuvo la aplicación aprobada en la que utilizó la misma? –

0

utilizar este método con la altura y la anchura respectiva para iPhone y iPad, respectivamente:

- (void)willPresentAlertView:(UIAlertView *)alertView { 
    [alertView setFrame:CGRectMake(5, 20, 300, 420)]; 
} 
Cuestiones relacionadas