UIAlertController 更改其他 UIView [英] UIAlertController change other UIView

查看:19
本文介绍了UIAlertController 更改其他 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间,但我不知道如何解决这个问题.我做了一个从底部出现的选择器视图.它工作正常,但问题是当任何其他事件显示警报视图时.同时选择器视图来自顶部.

i spend lot of time but i can not figure out how can fix this problem . I made a picker view that is appearing from bottom . it is working fine but problem is when any other event shown alert view . same time picker view come from top .

注意.在警报视图出现之前.选择器视图工作正常.出现警报视图后,派克视图出现在顶部.

note . before alert view appear . picker view working fine . after alert view appear then piker view come to the top .

底部出现piker视图代码:

bottom appear 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)
    }
}

推荐答案

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

You custom PickerView will always be on top of any other presented views because you are adding PickerView as a subview on MainWindow while you're presenting UIAlertController on current UIViewController.

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

Add your PickerView as a subview of current UIViewController instead of MainWindow, that will solve the problem.

如果您使用(现已弃用)UIAlertView,则不会发生此问题.但不幸的是 UIAlertView 现在已被弃用.

If you would have used (now deprecated) UIAlertView, this problem wouldn't have occurred. But unfortunately UIAlertView is now deprecated.

顺便说一句,在 MainWindow 上添加 PickerView 并不是一个很好的方法,所以以后避免使用.

And by the way, adding PickerView on MainWindow isn't a very good approach so avoid it in future.

希望有帮助!

这篇关于UIAlertController 更改其他 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆