IOS 15 实现Toast和小菊花Loading提示

本文主要是实现toast和loading两种提示功能,例如:登陆时参数不正确提示,toast提示后会自动隐藏。加载提示:不会自动隐藏,常用于网络请求,上传等。

添加依赖

#提示框架
#https://github.com/jdg/MBProgressHUD
pod 'MBProgressHUD'

还不了解如何使用 CocoaPods 管理依赖的,建议先看前面的文章:IOS 01 CocoaPods 安装与使用

添加完依赖后,看一下Pods文件夹里面是否添加成功。 

导入头文件

由于项目是由Swift写的,而MBProgressHUD是OC的,所以要在桥接文件中导入

//提示框架
#import <MBProgressHUD/MBProgressHUD.h>

定义SuperToast

//
//  SuperToast.swift
//  提示
//
//  Created by smile on 2022/7/6.
//

import UIKit

class SuperToast {
    static var hud:MBProgressHUD?
    
    /// 显示提示
    /// - Parameter title: <#title description#>
    static func show(title:String) {
        let hud = MBProgressHUD.showAdded(to: AppDelegate.shared.window!.rootViewController!.view, animated: true)
        hud.mode = .text
        
        //背景颜色
        hud.bezelView.style = .solidColor
        hud.bezelView.backgroundColor = .black
        
        //标题文字颜色
        hud.label.textColor = .colorLightWhite
        hud.label.font = UIFont.boldSystemFont(ofSize: 16)
        hud.label.numberOfLines = 0
        
        hud.label.text = title
        
        let offsetY = -hud.frame.height/CGFloat(2)+80
        
        //显示到屏幕顶部
        //因为显示到中心有点遮挡内容
        hud.offset = CGPoint(x: 0, y: offsetY)
        
        hud.removeFromSuperViewOnHide = true
        
        hud.hide(animated: true, afterDelay: 1.5)
    }
    
    /// 加载提示
    /// - Parameter title: <#title description#>
    static func showLoading(title:String = R.string.localizable.superLoading()) {
        //菊花颜色
        UIActivityIndicatorView.appearance(whenContainedInInstancesOf:
            [MBProgressHUD.self]).color = .white
        
        if SuperToast.hud == nil {
            SuperToast.hud = MBProgressHUD.showAdded(to: AppDelegate.shared.window!.rootViewController!.view, animated: true)
            SuperToast.hud!.mode = .indeterminate
            
            //最小尺寸
            SuperToast.hud!.minSize = CGSize(width: 120, height: 120)
            
            //背景半透明
            SuperToast.hud!.backgroundView.style = .solidColor
            SuperToast.hud!.backgroundView.color = UIColor(white: 0, alpha: 0.5)
            
            //背景颜色
            SuperToast.hud!.bezelView.style = .solidColor
            SuperToast.hud!.bezelView.backgroundColor = .black
            
            //标题文字颜色
            SuperToast.hud!.label.textColor = .colorLightWhite
            SuperToast.hud!.label.font = UIFont.boldSystemFont(ofSize: TEXT_LARGE)
            
            //显示对话框
            SuperToast.hud!.show(animated: true)
        }
        
        //设置对话框文字
        SuperToast.hud!.label.text = title
        
        //详细文字
//        SuperToast.hud!.detailsLabel.text = "请耐心等待"
    }
    
    /// 隐藏提示
    static func hideLoading() {
        if let r = SuperToast.hud {
            r.hide(animated: true)
            SuperToast.hud = nil
        }
    }
}

使用

SuperToast.show(title: "\(data.data.data?[0].title!)")

SuperToast.showLoading()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sziitjin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值