UIAlertController更改其他UIView

我花了很多时间,但我无法弄清楚如何解决这个问题。 我做了一个从底部出现的select器视图。 它工作正常,但问题是当任何其他事件显示警报视图。 同一时间select器视图从顶部。

注意 。 警报视图出现之前。 select器视图工作正常。 在警报视图出现之后,piker视图到达顶部。

这里提醒之前

底部出现piker view code:

extension RegistrationController { func showSettings(selecteIndex : Int) { //show menu if let window = UIApplication.shared.keyWindow { blackView.backgroundColor = UIColor(white: 0, alpha: 0.5) blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss))) window.addSubview(blackView) window.addSubview(bottomView) bottomView.addSubview(titlebottomView) titlebottomView.addSubview(bottomViewActionLabel) titlebottomView.addSubview(doneButton) let height: CGFloat = 150 let y = window.frame.height - height bottomView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height) titlebottomView.frame = CGRect(x: 0, y: 0, width: window.frame.width, height: 40) bottomView.addSubview(pikerViewForGender) bottomViewActionLabel.text = "Select your Gender" bottomView.addSubview(pikerViewForGender) pikerViewForGender.frame = CGRect(x: 0, y: 44, width: bottomView.frame.width, height: bottomView.frame.height - 40) self.bottomViewActionLabel.frame = CGRect(x: 13, y: 0, width: 150, height: 40) self.doneButton.frame = CGRect(x: window.frame.width - 80 , y: 0, width: 80, height: 40) blackView.frame = window.frame blackView.alpha = 0 UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { self.blackView.alpha = 1 self.bottomView.frame = CGRect(x: 0, y: y, width: window.frame.width, height: height) }, completion: nil) } } func handleDismiss() { UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { self.blackView.alpha = 0 if let window = UIApplication.shared.keyWindow { self.bottomView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: window.frame.height) } }) { (completed: Bool) in print("good way ....") } } } 

当显示警报

警报视图代码:

 extension UIViewController { func showAlert(message: String ){ let alert = UIAlertController(title: "Whoops", message: message, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(_ action: UIAlertAction) -> Void in alert.dismiss(animated: true, completion: nil) })) self.present(alert, animated: true, completion: nil) } } 

你完成的button作为子视图添加到titlebottomView 。 当你看到你的完成button,这意味着你的titlebottomView仍然在你的屏幕上,没有被解雇或带回来。 所以在你的handleDismiss()函数中,使用你想要的方法来确保titlebottomView在某个屏幕外的位置,或者把它作为最佳实践返回。

您自定义的PickerView将始终位于任何其他呈现的视图之上,因为您在当前的UIViewController上呈现UIAlertController同时,将在MainWindow上添加PickerView作为子视图。

添加你的PickerView作为当前UIViewController而不是MainWindow的子视图,这将解决问题。

如果你已经使用(现在不赞成) UIAlertView ,这个问题不会发生。 但不幸的是, UIAlertView现在已经被弃用了。

顺便说PickerViewMainWindow添加PickerView并不是一个很好的方法,所以在将来避免它。

希望能帮助到你!