简体   繁体   中英

Alert view center changes when another view is presented on root view

In the app, I'm showing an alert when there is no internet connection (Showing on root view controller). There is a Biometric authentication on the application too. So whenever Biometric page appears (Biometric page also shows on root view controller) on the top of the alertview and removes that from the view, alert view constraints changes and doesn't show in the middle.

Step 1:-

Shows Error message

在此处输入图片说明

Showing alert Code :-

  func showAlert(title:String, message: String, buttons: [UIAlertAction]) {
    // create the alert
    self.alert.title = title
    self.alert.message = message

    // add an action (button)
    if buttons.count == 0 {
        self.alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    } else {
        for i in 0...buttons.count-1 {
            self.alert.addAction(buttons[i])
        }
    }
    self.viewController.present(alert, animated: true, completion: nil)
}

Step 2:-

Exit the application and shows the biometric verification view.

App Delegate File :-

   func applicationDidBecomeActive(_ application: UIApplication) {

    if self.userToken != "" && self.biometricStatus && !UserAccessTemp.isBiometricActive {

        let controller = BiometricCheckViewController.instantiate(fromAppStoryboard: .BiometricCheck)
        if let window = self.window, let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            currentController.present(controller, animated: true, completion: nil)
        }

    }

在此处输入图片说明

Step 3:-

Alert View alignment changes after dismiss the biometric verification view

在此处输入图片说明

So how can I re-center the alert view after the Biometric view dismiss?

Just put makeKeyAndVisible() to the Biometric view, and it does fix the problem. Thanks to the Deepika's comment.

func applicationDidBecomeActive(_ application: UIApplication) {

    if self.userToken != "" && self.biometricStatus && !UserAccessTemp.isBiometricActive {

        let controller = BiometricCheckViewController.instantiate(fromAppStoryboard: .BiometricCheck)

        let alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow.rootViewController = UIViewController()
        alertWindow.windowLevel = UIWindowLevelAlert + 1;
        alertWindow.makeKeyAndVisible()
        alertWindow.rootViewController?.present(controller, animated: true, completion: nil)
    }
}

try:

func showAlert(title:String, message: String, buttons: [UIAlertAction]) {
    // create the alert
    self.alert.title = title
    self.alert.message = message

    // add an action (button)
    if buttons.count == 0 {
        self.alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    } else {
        for i in 0...buttons.count-1 {
            self.alert.addAction(buttons[i])
        }
    }

    if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }

        topController.present(alert, animated: true, completion: nil)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM