Search code examples
iosswiftswift4ios12

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

enter image description here

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

    }

enter image description here

Step 3:-

Alert View alignment changes after dismiss the biometric verification view

enter image description here

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


Solution

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