ios - 如何在 Swift 中创建 toast 消息?

标签 ios swift

有什么方法可以在 swift 中制作 toast 消息吗?

我在 objective c 中尝试过,但无法在 swift 中找到解决方案。

[self.view makeToast:@"Account created Successfully"
                     duration:0.5
                     position:@"bottom"];

最佳答案

extension UIViewController {

func showToast(message : String, font: UIFont) {

    let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
    toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    toastLabel.textColor = UIColor.white
    toastLabel.font = font
    toastLabel.textAlignment = .center;
    toastLabel.text = message
    toastLabel.alpha = 1.0
    toastLabel.layer.cornerRadius = 10;
    toastLabel.clipsToBounds  =  true
    self.view.addSubview(toastLabel)
    UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
         toastLabel.alpha = 0.0
    }, completion: {(isCompleted) in
        toastLabel.removeFromSuperview()
    })
} }

像这样使用:

self.showToast(message: "Your Toast Message", font: .systemFont(ofSize: 12.0))

关于ios - 如何在 Swift 中创建 toast 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540375/

相关文章:

arrays - 按 Int 数组对字符串数组进行排序

ios - 将图像同步上传到 Firebase

ios - Storyboard不包含带有标识符的 View Controller ......使用多个 Storyboard文件时

ios - 无法使用用户名和密码登录以在 Xmpp 中聊天

iOS:UIView 动画与 CABasic/CAKeyframe 动画的性能对比

ios - UITableViewController 手动完成

ios - AVPlayer 的定时元数据

ios - 在 iOS 应用程序中使用 GitHub 的 OAuth 身份验证 - 它是如何工作的

ios - 为什么这个委托(delegate)不触发是个谜

swift - 如何检查字符串是否包含 Swift 5 中的多个字符