【问题标题】:Swift: How can i avoid view overlapping in this situation斯威夫特:在这种情况下如何避免视图重叠
【发布时间】:2021-09-15 12:46:07
【问题描述】:

我是 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 是否为空,然后显示带有消息的视图。

在我第一次运行项目并单击带有空白字段的提交后,我得到了所需的结果,如此处所示。 Screenshot here

问题出现在我第二次按下提交按钮时,在我用一些文本完成字段后,因为它显示视图重叠。 Screenshot here

我正在尝试做的是一次显示一个“警报”,具体取决于情况。

下面是我的 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()
                                }
                                
                            })
                        }
                       })
    }
    
    
}

【问题讨论】:

  • customAlert 类中有 2 个函数...显示和关闭...尝试根据需要同时使用这两个函数...!您只是没有关闭警报视图

标签: ios swift xcode uikit


【解决方案1】:

每次调用showAlert 函数时,您似乎都在添加一个新的 UILabel。您应该将此标签添加为 alertView 的子标签,然后更改它的文本。

【讨论】:

    【解决方案2】:

    我设法通过在dismissAlert() 函数中添加以下两行代码来修复它:

    myTitleLabel?.text = ""
    myMessageLabel?.text = ""
    

    我也将这 2 个设置为全球性的。

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多