这似乎是特定于 iOS 13.1 的,因为它在 iOS 13.0 和更早版本上按预期工作以在 CNContactViewController 中添加联系人,如果我“取消”,则操作表会被键盘重叠。没有执行任何操作,键盘也没有解除。
最佳答案
感谢@GxocT 的出色解决方法!极大地帮助了我的用户。
但我想分享我基于@GxocT 解决方案的代码,希望它能在这种情况下帮助其他人。
我需要我的 CNContactViewControllerDelegate
contactViewController(_:didCompleteWith:)
在取消时调用(以及完成)。
另外我的代码不在 UIViewController
中所以没有 self.navigationController
当我可以帮助它时,我也不喜欢使用强制展开。我以前被咬过所以我用铁链锁住了if let
在设置中
这是我所做的:
CNContactViewController
并将 swizzle 功能放在那里。
CNContactViewControllerDelegate
代表contactViewController(_:didCompleteWith:)
与 self
和self.contact
来自联系人 Controller 的对象 class_getInstanceMethod
指定 CNContactViewController
类而不是 self
和 Swift 代码:
class MyClass: CNContactViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.changeImplementation()
}
func changeCancelImplementation() {
let originalSelector = Selector(("editCancel:"))
let swizzledSelector = #selector(CNContactViewController.cancelHack)
if let originalMethod = class_getInstanceMethod(object_getClass(CNContactViewController()), originalSelector),
let swizzledMethod = class_getInstanceMethod(object_getClass(CNContactViewController()), swizzledSelector) {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
// dismiss the contacts controller as usual
viewController.dismiss(animated: true, completion: nil)
// do other stuff when your contact is canceled or saved
...
}
}
extension CNContactViewController {
@objc func cancelHack() {
self.delegate?.contactViewController?(self, didCompleteWith: self.contact)
}
}
键盘仍会暂时显示,但会在 Contacts Controller 关闭后立即下降。
希望苹果解决这个问题
关于swift - CNContactViewController 上 iOS 13.1 中的键盘覆盖操作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178882/