我对 swift 和 xcode 很陌生,当我修改应用程序制作时,遇到了一个问题。我正在尝试创建一个按钮,在使用 FaceID 验证我的脸部后切换 View Controller 。这是我到目前为止所拥有的。当我按下按钮时,它确实进行了身份验证,但是当它转到另一个窗口时,标签和按钮就消失了。有人知道为什么吗?任何事情都有帮助。谢谢(:
注意:尽管该按钮不可见,但它仍然有效。我知道这一点是因为当我在 Controller2 中并点击左上角的按钮时,它就完成了操作。问题很简单,就是它不可见。
左边是Controller1,右边是Controller2
代码:
@IBAction func faceIDButton(_ sender: Any) {
let context = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "To sign in") { (wasSuccessful, errorInCode) in
if wasSuccessful {
self.performSegue(withIdentifier: "toSecondView", sender: self)
}
}
}
}
最佳答案
首先您需要以模态方式呈现 vc 并使其主视图透明,只有完整的部分是您通过此识别脸部的矩形
let vc = self.storyboard!.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
vc.delegate = self //to send success of face detection
vc.providesPresentationContextTransitionStyle = true
vc.definesPresentationContext = true
vc.modalPresentationStyle = .overCurrentContext // modally key line
self.present(vc, animated: false, completion: nil)
您还可以在 IB 中为第二个 VC 设置这些属性,或者在由 performSegue
触发的显示转场类型的 prepareForSegue
中设置它们
第二个不要从第二个 vc 到第一个 vc,您只需要使用
self.dismiss(animated:true,completion:nil)
最后evaluatePolicy
的完成在后台线程中运行,因此如果您要使用segue,请考虑这一点
DispatchQueue.main.async {
self.performSegue(withIdentifier: "toSecondView", sender: self)
}
关于ios - 按钮和标签在segue后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54485132/