Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用Swift创建带有按钮的自定义视图?_Ios_Swift - Fatal编程技术网

Ios 如何使用Swift创建带有按钮的自定义视图?

Ios 如何使用Swift创建带有按钮的自定义视图?,ios,swift,Ios,Swift,在我的例子中,我试图创建toast消息弹出窗口,使用自定义UI设计,如下图所示。它应该是带有按钮操作的公共类,并且应该允许从其他视图控制器调用,并传递一些消息和时间间隔参数。我正在用.XIB查找一些启动代码库。请帮助我实现这一目标 预期的Toast UI 它未经测试,但可能会对您有所帮助 func showToast(message : String) { // add label let toastLabel = UILabel(frame: CGRect(x:

在我的例子中,我试图创建
toast
消息弹出窗口,使用
自定义UI
设计,如下图所示。它应该是带有按钮操作的公共类,并且应该允许从其他
视图控制器调用
,并传递一些消息和时间间隔
参数
。我正在用
.XIB
查找一些启动代码库。请帮助我实现这一目标

预期的Toast UI


它未经测试,但可能会对您有所帮助

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视图并添加为子视图