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

来源:互联网 发布:cpa单页源码 编辑:程序博客网 时间:2024/06/04 23:35

在这我直接介绍需要添加哪些代码,可直接复制黏贴来使用。我是特意创建的 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 = [[UIApplicationsharedApplication].windowsobjectAtIndex:1];

    UIView *showview =  [[UIViewalloc]init];

    showview.backgroundColor = [UIColorblackColor];

    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 = [[UILabelalloc]init];

    CGSize LabelSize = [messagesizeWithFont:[UIFontsystemFontOfSize:font+2]constrainedToSize:CGSizeMake(290,9000)];

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

    label.text = message;

    label.textColor = [UIColorwhiteColor];

    label.textAlignment =1;

    label.backgroundColor = [UIColorclearColor];

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

    label.font = [UIFontsystemFontOfSize:font];

    [showview addSubview:label];

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

    [UIViewanimateWithDuration:2animations:^{

        showview.alpha =0;

    } completion:^(BOOL finished) {

        [showview removeFromSuperview];

    }];

}

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

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



0 0
原创粉丝点击