【问题标题】:How to create Custom Toast UIView with Button using Swift?如何使用 Swift 创建带有按钮的自定义 Toast UIView?
【发布时间】:2019-10-08 10:28:52
【问题描述】:

就我而言,我正在尝试使用custom UI 设计创建toast 消息弹出窗口,如下图示例所示。它应该是带有按钮操作的通用类,它应该允许从其他ViewControllers 调用,其中一些消息和时间间隔通过parameters。我正在寻找一些带有.XIB 的初始代码库。请帮助我实现这一目标。

预期的 Toast 用户界面

【问题讨论】:

  • @Sh_Khan 我会发布我正在尝试的内容。

标签: ios swift


【解决方案1】:

它没有经过测试,但它可能会帮助你

func showToast(message : String) {

      // add label
        let toastLabel = UILabel(frame: CGRect(x: 30, y: self.view.frame.size.height-150, width: 330, height: 35))
        toastLabel.text = message
        toastLabel.alpha = 1.0
        toastLabel.layer.cornerRadius = 10;
        toastLabel.clipsToBounds  =  true

        //add button 
        let button = UIButton.init(type: .custom)
        button.frame = CGRect.init(x: toastLabel.frame.width - 50, y: toastLabel.frame.origin.y + 20, width: 40, height: 40)
        self.view.addSubview(toastLabel)
        self.view.addSubview(button)
        UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
            toastLabel.alpha = 0.0
        }, completion: {(isCompleted) in
            toastLabel.removeFromSuperview()
        })
    }

【讨论】:

  • 谢谢。顺便说一句,是否可以加载 .Xib?
  • 是的,您可以直接在此处加载您的 xib 视图并添加为子视图
猜你喜欢
  • 2018-01-03
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多