Android Toast相当于iOS

Bil*_*mes 28 android toast xamarin.ios ios xamarin

有谁知道这个iOS Objective C事件的Java Toast等价物是什么?以下是我在iOS中编写的示例.我正在寻找使用Toast代替iOS UIAlert的Java中的相同警报.如果我在原帖上没说清楚,我很抱歉.

- (void) dateLogic {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMMM dd"];
    NSString *theDate = [dateFormat stringFromDate:[NSDate date]];

    //JANUARY
    if ([theDate isEqualToString:@"January 01"]) {

        feastDay = [[UIAlertView alloc]
                     initWithTitle:@"New Years Day!"
                     message:@"January 01"
                     delegate:self
                     cancelButtonTitle:nil
                     otherButtonTitles:@"Close", nil];
        feastDay.delegate = self;
        [feastDay show];
    }
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*lli 30

我在github中发现了这个令人惊叹的课程,就像一个魅力. iOS的Toast 足以导入UIView + Toast.h和UIView + Toast.m文件,然后添加

[self.view makeToast:@"This is a piece of toast."];
Run Code Online (Sandbox Code Playgroud)

如在页面示例中所写.

  • 同一个库的Swift端口:https://github.com/scalessec/Toast-Swift (2认同)

小智 12

我使用Key Window使用简单的静态UI Helper方法处理它:

+(void)displayToastWithMessage:(NSString *)toastMessage
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
        UILabel *toastView = [[UILabel alloc] init];
        toastView.text = toastMessage;
        toastView.font = [MYUIStyles getToastHeaderFont];
        toastView.textColor = [MYUIStyles getToastTextColor];
        toastView.backgroundColor = [[MYUIStyles getToastBackgroundColor] colorWithAlphaComponent:0.9];
        toastView.textAlignment = NSTextAlignmentCenter;
        toastView.frame = CGRectMake(0.0, 0.0, keyWindow.frame.size.width/2.0, 100.0);
        toastView.layer.cornerRadius = 10;
        toastView.layer.masksToBounds = YES;
        toastView.center = keyWindow.center;

        [keyWindow addSubview:toastView];

        [UIView animateWithDuration: 3.0f
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations: ^{
                         toastView.alpha = 0.0;
                     }
                     completion: ^(BOOL finished) {
                         [toastView removeFromSuperview];
                     }
         ];
    }];
}
Run Code Online (Sandbox Code Playgroud)


Viv*_*ivo 11

在iOS中没有Android toast等效.

但总有一些解决方法

您可以为视图设置动画并使用其Alpha进行播放

以下只是示例代码而非解决方案

UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3.0f];
imageView.alpha = 0.0f;
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

如果你不想在3秒内慢慢褪色,你可以使用

[UIView setAnimationDelay:3];
Run Code Online (Sandbox Code Playgroud)

并将动画延伸减少到0.5f或其他东西.我认为使用短暂的淡出时间比仅仅将hide设置为YES更好


ear*_*ian 11

我们在开源库Raisin Toast中实现了类似的功能.将文件添加到项目后,安装程序非常简单:

将属性添加到您的应用委托:

@property (strong, nonatomic) RZMessagingWindow *errorWindow;
Run Code Online (Sandbox Code Playgroud)

创建默认消息传递窗口:

self.errorWindow = [RZMessagingWindow defaultMessagingWindow];
[RZErrorMessenger setDefaultMessagingWindow:self.errorWindow];
Run Code Online (Sandbox Code Playgroud)

然后一行显示消息窗口:

[RZErrorMessenger displayErrorWithTitle:@"Whoops!" detail:@"Something went horribly wrong and we accidentally cut off the wrong leg"];
Run Code Online (Sandbox Code Playgroud)

该演示项目重点介绍了一些添加图像和自定义样式的高级自定义.

该实现使用第二个UIWindow,并且由于RZErrorMessenger类方法,它随处可用.


Dan*_* D. 9

也许这可以帮助某人:

NSString *message = @"Toast kind of message";
UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                            message:message
                                           delegate:nil
                                  cancelButtonTitle:nil
                                  otherButtonTitles:nil, nil];
[toast show];
int duration = 1; // in seconds

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[toast dismissWithClickedButtonIndex:0 animated:YES];
});
Run Code Online (Sandbox Code Playgroud)

更新 UIAlertView在IOS 8中已弃用.以下是新方法:

NSString *message = @"Toast kind of message";

UIAlertController *toast =[UIAlertController alertControllerWithTitle:nil
 message:message 
 preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:toast animated:YES completion:nil];

int duration = 1; // in seconds

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [toast dismissViewControllerAnimated:YES completion:nil];
});
Run Code Online (Sandbox Code Playgroud)

编辑: 对于那些使用Xamarin.IOS你可以这样做:

new UIAlertView(null, message, null, "OK", null).Show();
Run Code Online (Sandbox Code Playgroud)

使用UIKit; 是必须的.