我是Swift和XCode的新手,这可能是一个愚蠢的问题,但我不能自己弄清楚:(
这是我的LoginViewController看起来像。Image1
当我按下提交按钮时,我有以下函数触发。
@objc func loginButtontapped(){
if emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
customAlert.showAlert(with: "Error", message: "Fill in the fields.", on: self)
} else {
let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
Auth.auth().signIn(withEmail: email, password: password) { (result, err) in
if err != nil {
self.customAlert.showAlert(with: "Error", message: "Invalid email or password.", on: self)
}else{
let homepageVC = HomepageViewController()
homepageVC.modalPresentationStyle = .fullScreen
let transition = CATransition()
transition.duration = 0.3
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromLeft
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
self.view.window!.layer.add(transition, forKey: kCATransition)
self.present(homepageVC, animated: false, completion: nil)
}}}}
基本上它会验证TextFields是否为空,然后显示一个带有消息的视图。
在我第一次运行项目并点击带有空字段的Submit后,我得到了想要的结果,如下所示。截图在这里
问题出现在我第二次按下提交按钮时,在我用一些文本完成字段之后,因为它显示视图重叠。截图在这里
我要做的是显示一个"警报"一次,视情况而定。
下面是我的CustomAlert是如何构建的。
class CustomAlert{
struct Constants {
static let backgroundAlphaTo: CGFloat = 0.6
}
private let backgroundView: UIView = {
let backgroundView = UIView()
backgroundView.backgroundColor = .black
backgroundView.alpha = 0
return backgroundView
}()
private let alertView: UIView = {
let alertView = UIView()
alertView.backgroundColor = UIColor(rgb: 0x363636, alphaVal: 1)
alertView.layer.masksToBounds = true
alertView.layer.cornerRadius = 12
return alertView
}()
private var myTargetView: UIView?
func showAlert(with title: String, message: String, on ViewController: UIViewController){
guard let targetView = ViewController.view else{
return
}
myTargetView = targetView
backgroundView.frame = targetView.bounds
targetView.addSubview(backgroundView)
targetView.addSubview(alertView)
alertView.frame = CGRect(x: 40,
y: -300,
width: targetView.frame.size.width-80,
height: 180)
let titleLabel = UILabel(frame: CGRect(x: 20,
y: 10,
width: alertView.frame.size.width,
height: 40))
titleLabel.text = title
titleLabel.textAlignment = .left
titleLabel.font = UIFont(name: "Roboto-Regular", size: 20)
titleLabel.textColor = .white
alertView.addSubview(titleLabel)
let messageLabel = UILabel(frame: CGRect(x: 20,
y: 40,
width: alertView.frame.size.width-30,
height: 60))
messageLabel.text = message
messageLabel.font = UIFont(name: "Roboto-Thin", size: 18)
messageLabel.textColor = .white
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .left
alertView.addSubview(messageLabel)
let button = UIButton (frame: CGRect(x: alertView.frame.size.width-130,
y: alertView.frame.size.height-70,
width: (alertView.frame.midX)/2,
height: 50))
button.setTitle("Okay", for: .normal)
button.applyGradient(colors: [Helper.UIColorFromRGB(0x694BCE).cgColor,Helper.UIColorFromRGB(0x7D3297).cgColor])
button.addTarget(self, action: #selector(dismissAlert), for: .touchUpInside)
alertView.addSubview(button)
UIView.animate(withDuration: 0.25, animations: {
self.backgroundView.alpha = Constants.backgroundAlphaTo},
completion: {done in
if done{
UIView.animate(withDuration: 0.25, animations: {
self.alertView.center = targetView.center
})
}
})
}
@objc func dismissAlert(){
guard let targetView = myTargetView else{
return
}
UIView.animate(withDuration: 0.25, animations: {
self.alertView.frame = CGRect(x: 40,
y: targetView.frame.size.height,
width: targetView.frame.size.width-80,
height: 300)
},
completion: { [weak self] done in
if done{
UIView.animate(withDuration: 0.25, animations: {
self?.backgroundView.alpha = 0
}, completion: { done in
if done {
self?.alertView.removeFromSuperview()
self?.backgroundView.removeFromSuperview()
}
})
}
})
}
}
看起来您每次调用showAlert
函数时都添加了一个新的UILabel。你应该将这个标签添加为alertView
的子标签,并更改它的文本。
我设法通过在dismissAlert()函数中添加以下两行代码来修复它:
myTitleLabel?.text = ""
myMessageLabel?.text = ""
我还把这两个设置为全局