2017-02-21 110 views
0

我正在尝试使用PKHUD创建自定义视图,以便在API调用失败时,我可以向用户显示一条消息以及一个按钮以寻求帮助。在swift中使用PKHUD创建自定义视图3

这里是我的类文件:

import UIKit 
import PKHUD 

class PKHUDHelpDeskView: PKHUDWideBaseView { 

    private func callNumber(phoneNumber:String) { 
     if let phoneCallURL = URL(string: "tel:\(phoneNumber)") { 
      let application:UIApplication = UIApplication.shared 
      if (application.canOpenURL(phoneCallURL)) { 
       if #available(iOS 10.0, *) { 
        application.open(phoneCallURL, options: [:], completionHandler: nil) 
       } else { 
        // Fallback on earlier versions 
       } 
      } 
     } 
    } 

    @IBAction func helpDeskNumberButton(_ sender: Any) { 
     callNumber(phoneNumber: "8005551234") 
    } 
} 

这是我如何我称之为:

PKHUD.sharedHUD.contentView = PKHUDHelpDeskView() 
PKHUD.sharedHUD.show() 
PKHUD.sharedHUD.hide(afterDelay: 4.0) 

我在故事板上设置了一个视图(设置为PKHUDHelpDeskView类)与按钮和显示消息的文本字段。当我运行这个时,PKHUD显示没有文字。类文件和故事板已正确连接,因此需要做些什么来使文本显示在PKHUD中?

回答

1

我试着做同样的事情。我没有添加UILabelUIButton使用故事板(我相信你在这里指的是.xib),我用编程方式添加它们。以下是我的最终代码。请尝试一下

import PKHUD 

class PKHUDHelpDeskView: PKHUDWideBaseView { 

    let button: UIButton = UIButton(type: UIButtonType.custom) 
    let label: UILabel = UILabel() 

    override func didMoveToSuperview() { 
     super.didMoveToSuperview() 

     button.setTitle("Call", for: UIControlState.normal) 
     button.backgroundColor = UIColor.red 
     button.addTarget(self, action: #selector(self.helpDeskNumberButton(_:)), for: UIControlEvents.touchUpInside) 

     label.text = "Call me now" 
     label.textColor = UIColor.brown 
     label.font = UIFont.systemFont(ofSize: 16) 
     label.textAlignment = NSTextAlignment.center 

     self.addSubview(label) 
     self.addSubview(button) 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     self.button.frame = CGRect(x: 0, y: 0, width: self.frame.size.width/2, height: 30.0) 
     self.label.frame = CGRect(x: 0, y: 30.0, width: self.frame.size.width, height: 40.0) 
    } 

    private func callNumber(phoneNumber:String) { 
     if let phoneCallURL = URL(string: "tel:\(phoneNumber)") { 
      let application:UIApplication = UIApplication.shared 
      if (application.canOpenURL(phoneCallURL)) { 
       if #available(iOS 10.0, *) { 
        application.open(phoneCallURL, options: [:], completionHandler: nil) 
       } else { 
        // Fallback on earlier versions 
       } 
      } 
     } 
    } 

    func helpDeskNumberButton(_ sender: Any) { 
     callNumber(phoneNumber: "8005551234") 
    } 
} 
+0

谢谢!我不确定它为什么以编程方式工作,而不是在故事板中,但对我来说没问题。我已经格式化了文本和按钮,并且正是我所需要的,但按钮不起作用。在阻止我能够按下按钮的所有东西上都有一个视图。 [见这里...](http://imgur.com/A4eF9ZA)。有任何想法吗? – Anonymoose