我是Swift和XCode的新手,這可能是一個愚蠢的問題,但我自己也搞不清楚:(
)我的LoginViewController看起來是這樣的。 Image1/a>
我有以下函式,當我按下提交按鈕時就會觸發。
@objc func loginButtontapped(){
if emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||>
passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
customAlert.showAlert(with: "Error", message: "填寫欄位。", 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: "無效的電子郵件或密碼。", 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(hepageVC, animated: false, completion: nil)
}}}}
基本上,它驗證了TextFields是否為空,之后它顯示了一個帶有訊息的視圖。
在我運行該專案后,第一次在空欄位的情況下點擊提交,我得到了想要的結果,在這里可以看到。這里的螢屏截圖
問題出現在我第二次按提交按鈕時,在我用一些文字完成欄位后,因為它顯示了視圖的重疊。 螢屏截圖
我想做的是,根據情況,一次顯示一個 "警報"。
下面是我的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.viewelse{
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。
高度。180)
let titleLabel = UILabel( frame: CGRect(x: 20,
y: 10,
width: alertView.frame.size.width,
高度。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。
高度。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。
寬度:(alertView.frame.midX)/2。
高度。50)
button.setTitle("Okay", for: .normal)
button.applyGradient(color: [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},
完成。{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。
高度。300)
},
完成。{ [weak self] done in
if done{
UIView.animate(withDuration: 0.25, animations: {
self?.backgroundView.alpha = 0
},完成。{ done in
if done {
self? .alertView.removeFromSuperview()
self?.backgroundView.removeFromSuperview()
}
})
}
})
}
}
uj5u.com熱心網友回復:
看起來你在每次呼叫showAlert函式時都會添加一個新的UILabel。你應該把這個標簽作為alertView的子節點,然后改變它的文本。
uj5u.com熱心網友回復:
我通過在 dismissAlert() func 中添加以下兩行代碼來解決這個問題:
'''
myTitleLabel?.text = "" myMessageLabel?.text = ""
'''
我還把這2個字做成了全域的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/306773.html
標籤:
下一篇:iOS組合式退火手動
