UIAlertController change other UIView

北慕城南 提交于 2019-12-13 10:22:37

问题


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 .

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 ....")

        }
    }

}

alert view code :

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

回答1:


Your done button is added as subview onto titlebottomView. When you see your done button up there, it means that your titlebottomView is still on your screen, not getting dismissed or brought back. So in your handleDismiss() function, use your desired way to make sure titlebottomView is in some off screen location or bring it back as best practice.




回答2:


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.

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

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

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

Hope it helps!



来源:https://stackoverflow.com/questions/44612259/uialertcontroller-change-other-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!