当另一个视图出现在根视图上时,警报视图中心会发生变化

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

在应用程序中,当没有 互联网连接 时,我会显示警报(在根视图控制器上显示)。应用程序上也有一个 Biometric authentication。因此,每当 alertview 顶部出现生物识别页面(生物识别页面也显示在根视图控制器上)并将其从视图中删除时,警报视图 constraints 会更改并且不会显示在中间。

第一步:-

显示错误消息

显示警报代码:-

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

第 2 步:-

退出应用程序并显示生物识别验证视图。

应用程序委托文件:-

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

    }

第 3 步:-

关闭生物特征验证视图后警告视图对齐方式发生变化

那么在生物识别视图关闭后如何重新居中警报视图?

只需将 makeKeyAndVisible() 放入生物识别视图,即可解决问题。感谢 Deepika 的评论。

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

尝试:

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