天天看点

Swift开发---toast 界面底部显示的提示框

在这我直接介绍需要添加哪些代码,可直接复制黏贴来使用。我是特意创建的 Tool.h Tool.m 来放oc代码公用的方法,要是放在.swift就不用这么麻烦了  可以直接使用。

1. 在  Tool.h  文件中添加如下代码:

//类似安卓提示框

+(void)showMessage:(NSString *)message  withFont:(CGFloat)font;

2.在 Tool.m 文件中添加如下代码:

#pragma mark -- 提示框

+(void)showMessage:(NSString *)message  withFont:(CGFloat)font

{

//    UIWindow * window = [UIApplication sharedApplication].keyWindow;

    UIWindow * window = [[UIApplication sharedApplication].windows objectAtIndex:1];

    UIView *showview =  [[UIView alloc]init];

    showview.backgroundColor = [UIColor blackColor];

    showview.frame = CGRectMake(1, 1, 1, 1);

    showview.alpha = 1.0f;

    showview.layer.cornerRadius = 5.0f;

    showview.layer.masksToBounds = YES;

    [window addSubview:showview];

    [window bringSubviewToFront:showview];

    UILabel *label = [[UILabel alloc]init];

    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:font+2] constrainedToSize:CGSizeMake(290, 9000)];

    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);

    label.text = message;

    label.textColor = [UIColor whiteColor];

    label.textAlignment = 1;

    label.backgroundColor = [UIColor clearColor];

    //    label.font = [UIFont boldSystemFontOfSize:font];//需要使用加粗字体文字可打开这行,注释

    label.font = [UIFont systemFontOfSize:font];

    [showview addSubview:label];

    showview.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - LabelSize.width - 20)/2, [UIScreen mainScreen].bounds.size.height - 100, LabelSize.width+20, LabelSize.height+10);

    [UIView animateWithDuration:2 animations:^{

        showview.alpha = 0;

    } completion:^(BOOL finished) {

        [showview removeFromSuperview];

    }];

}

3.在别的viewcontroller.swift 调用的代码如下:

Tool.showMessage("输入你想要显示的文字!", withFont: 15)