Keyboard overlaying action sheet in iOS 13.1 on CNContactViewController

后端 未结 7 2152
你的背包
你的背包 2020-12-15 23:58

This seems to be specific to iOS 13.1, as it works as expected on iOS 13.0 and earlier versions to add a contact in CNContactViewController, if I \'Cancel\', the action shee

7条回答
  •  萌比男神i
    2020-12-16 00:02

    Thanks, @GxocT for your workaround, however, the solution posted here is different from the one you posted on Reddit.

    The one on Reddit works for me, this one doesn't so I want to repost it here. The difference is on the line with swizzledMethod which should be:

       let swizzledMethod = class_getInstanceMethod(object_getClass(self), swizzledSelector) {
    

    The whole updated code is:

    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(self), 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)
        }
    }
    

提交回复
热议问题