He creado una tostada Android-Kind, bastante simple, ya que no necesito más funcionalidad en ella por ahora.
Cuando se muestra, se agrega en la parte inferior de la vista de los padres, por lo que si esa es la vista de VC, entonces estará en la parte inferior central del dispositivo.
El marco se autoajusta a la longitud del texto.
Lo usa haciendo: [self.view addSubview: [[ToastAlert alloc] initWithText: @"Sent"]];
, se eliminará automáticamente por lo que no es necesario hacer referencia.
No lo he implementado, pero puede crear un método estático para acortar y aclarar las instrucciones, tipo de: [ToastAlert showText: @"Sent" inView: self.view];
.
La clase:
ToastAlert.h
@interface ToastAlert : UILabel {
}
- (id)initWithText: (NSString*) msg;
@end
ToastAlert.m
#import "ToastAlert.h"
#import <QuartzCore/QuartzCore.h>
@implementation ToastAlert
#define POPUP_DELAY 1.5
- (id)initWithText: (NSString*) msg
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
self.textColor = [UIColor colorWithWhite:1 alpha: 0.95];
self.font = [UIFont fontWithName: @"Helvetica-Bold" size: 13];
self.text = msg;
self.numberOfLines = 0;
self.textAlignment = UITextAlignmentCenter;
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
}
return self;
}
- (void)didMoveToSuperview {
UIView* parent = self.superview;
if(parent) {
CGSize maximumLabelSize = CGSizeMake(300, 200);
CGSize expectedLabelSize = [self.text sizeWithFont: self.font constrainedToSize:maximumLabelSize lineBreakMode: NSLineBreakByTruncatingTail];
expectedLabelSize = CGSizeMake(expectedLabelSize.width + 20, expectedLabelSize.height + 10);
self.frame = CGRectMake(parent.center.x - expectedLabelSize.width/2,
parent.bounds.size.height-expectedLabelSize.height - 10,
expectedLabelSize.width,
expectedLabelSize.height);
CALayer *layer = self.layer;
layer.cornerRadius = 4.0f;
[self performSelector:@selector(dismiss:) withObject:nil afterDelay:POPUP_DELAY];
}
}
- (void)dismiss:(id)sender {
// Fade out the message and destroy self
[UIView animateWithDuration:0.6 delay:0 options: UIViewAnimationOptionAllowUserInteraction
animations:^ { self.alpha = 0; }
completion:^ (BOOL finished) { [self removeFromSuperview]; }];
}
@end
Esto es lo más cercano a lo que estaba buscando, aunque terminé escribiendo el mío. – David
Encontré ALToastView útil, gracias! –
Actualización: El siempre popular [MBProgressHUD] (https://github.com/jdg/MBProgressHUD) ahora tiene una [notificación similar a un brindis] (http://dl.dropbox.com/u/378729/MBProgressHUD/7. png). –