
IOS
Swiftfunc showToast(message: String) { let toastLabel = UILabel(frame: CGRect(x: 20, y: view.frame.size.height-100, width: view.frame.size.width-40, height: 35)) toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6) toastLabel.textColor = UIColor.white toastLabel.font = UIFont.systemFont(ofSize: 14) toastLabel.textAlignment = .center toastLabel.text = message toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10 toastLabel.clipsToBounds = true let toastContAIner = UIView(frame: CGRect(x: view.frame.size.width/2 - (toastLabel.frame.width/2), y: view.frame.size.height-100, width: toastLabel.frame.width, height: toastLabel.frame.height)) toastContAIner.backgroundColor = UIColor.clear toastContAIner.addSubview(toastLabel) view.addSubview(toastContAIner) UIView.animate(withDuration: 2.0, delay: 0.1, options: .curveEaSEOut, animations: { toastLabel.alpha = 0.0 }, completion: {(isCompleted) in toastContAIner.removeFromSuperview() })}// 使用示例showToast(message: "这是一个Toast消息")上述代码中的showToast函数可以在任意的视图控制器中调用,用来展示Toast消息。只需提供消息的文本内容,即可在屏幕底部显示一个类似于Android Toast的通知。通过结合使用UILabel和UIView,我们可以在IOS中实现类似于Android Toast的通知效果。尽管在IOS中没有直接的对应项,但通过编写一些简单的代码,我们可以轻松地实现类似的功能,为用户提供更好的使用体验。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号