我正在尝试使用PKHUD创建一个自定义视图,以便当API调用失败时,我可以向用户显示一条消息以及一个用于调用帮助的按钮。
这是我的班级档案:
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中?
最佳答案
我也试过这么做。不是使用故事板来添加UILabel
和UIButton
(我相信你指的是。这里是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")
}
}
关于ios - 在Swift 3中使用PKHUD创建自定义 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42371994/