swift - CNContactViewController 上 iOS 13.1 中的键盘覆盖操作表

标签 swift ios13 cncontact cncontactstore cncontactviewcontroller

这似乎是特定于 iOS 13.1 的,因为它在 iOS 13.0 和更早版本上按预期工作以在 CNContactViewController 中添加联系人,如果我“取消”,则操作表会被键盘重叠。没有执行任何操作,键盘也没有解除。

最佳答案

感谢@GxocT 的出色解决方法!极大地帮助了我的用户。
但我想分享我基于@GxocT 解决方案的代码,希望它能在这种情况下帮助其他人。

我需要我的 CNContactViewControllerDelegate contactViewController(_:didCompleteWith:)在取消时调用(以及完成)。

另外我的代码不在 UIViewController 中所以没有 self.navigationController
当我可以帮助它时,我也不喜欢使用强制展开。我以前被咬过所以我用铁链锁住了if let在设置中

这是我所做的:

  • 扩展 CNContactViewController并将 swizzle 功能放在
    那里。
  • 在我的情况下,在 swizzle 函数中只需调用CNContactViewControllerDelegate代表contactViewController(_:didCompleteWith:)selfself.contact来自联系人 Controller 的对象
  • 在设置代码中,确保 swizzleMethod 调用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/

    相关文章:

    swift - CNContactPickerViewController 中的多重选择

    ios - x 轴下方的刻度线(iOS 图表)

    ios - Swift/iOS13 - 来自自定义类的 UIApplication.shared.delegate.window?.rootViewController?.present()

    ios - 如何从iOS中的CNContactProperty获取国家/地区代码

    html - iOS 13 Swift 5 wkwebview - 将文档目录中的图像显示到 wkwebview 真实设备中

    iOS 13 NSAttributedString NSKeyedArchiver.archivedData 丢失属性

    ios - 是否可以为 CNContactViewController 添加自定义导航栏?

    ios - 在运行时获取 UIImageView IB 图像属性值

    swift - 检查加载的 WebView 的 URL

    swift - 如何在 swift UITests 的 XCUIApplication 中设置暗模式?