Display CNContactPickerViewController in Container View?

1.3k Views Asked by At

I'm currently updating an old Xcode project I started working on a long time ago. I updated the deployment target to iOS 10.0 so I was forced to replace the ABAddressBook framework with Contacts framework. I used to present the ABPeoplePickerNavigationController inside a Container View, which was a subview of the root view controller of a UINavigationController. That way I was able to present the People Picker UI inside a navigation controller, which was inside a tab bar controller (which meant that the navigation bar at the top and the tab bar at the bottom were still displayed around the people picker UI). I used to do it with this code (called in viewWillAppear of navigation controller's root view controller:

let peoplePickerNavController:ABPeoplePickerNavigationController = ABPeoplePickerNavigationController()
    self.addChildViewController(peoplePickerNavController)
    peoplePickerNavController.didMove(toParentViewController: self)
    peoplePickerNavController.navigationBar.removeFromSuperview()
    peoplePickerNavController.view.frame = self.peoplePickerContainerView.bounds
    peoplePickerNavController.view.translatesAutoresizingMaskIntoConstraints = true
    peoplePickerNavController.peoplePickerDelegate = self
    self.peoplePickerContainerView.addSubview(peoplePickerNavController.view)

With this code, it would display the address book perfectly with the navigation bar of the navigation controller still at the top and the tab bar of the tab bar controller still at the bottom. I'm trying to do the same thing now with CNContactPickerViewController, but I'm having a little trouble with this one. When I do the same thing I did with the navigation controller, it doesn't give me any errors but the view controller is not being displayed. Even though I'm adding the view of the view controller to my container view, it's just showing an empty white view. This is the code I'm using now:

let contactPickerViewController:CNContactPickerViewController = CNContactPickerViewController()
    self.addChildViewController(contactPickerViewController)
    contactPickerViewController.didMove(toParentViewController: self)
    contactPickerViewController.view.frame = self.contactPickerContainerView.bounds
    contactPickerViewController.view.translatesAutoresizingMaskIntoConstraints = true
    contactPickerViewController.delegate = self
    self.contactPickerContainerView.addSubview(contactPickerViewController.view)

The only significant difference between those two codes is that I'm not removing the navigation bar from the view controller (because it doesn't have one). Other than that, I left everything pretty much the same. I'm guessing I need to change a few other things in order to make this work, since there is a difference between adding a navigation controller to the Container View and adding a View Controller. Can anyone help me out and give me some tips on how to achieve this? Thank you!

1

There are 1 best solutions below

0
On

Use this code to display contactPickerViewController inside tabBar

let contactPickerViewController:CNContactPickerViewController = CNContactPickerViewController()
contactPickerViewController.modalPresentationStyle = .overCurrentContext
self.presentViewController(contactPicker, animated: false, completion: nil)