Display CNContactPickerViewController in Container View? - ios

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!

Use this code to display contactPickerViewController inside tabBar
let contactPickerViewController:CNContactPickerViewController = CNContactPickerViewController()
contactPickerViewController.modalPresentationStyle = .overCurrentContext
self.presentViewController(contactPicker, animated: false, completion: nil)

Related

Bar item button not displayed at runtime but visible at design time in a modal view

I have being trying different things and looking around for a while without finding an answer to my problem. Maybe I'm doing something fundamentally wrong.
The sample application consists of:
A first view controller that displays a second view controller using a segue. This works fine.
A second view controller, in which I have simulated the display of a third view programmatically, which contains a bar item button (named "Done") that I would like to display.
The bar item button in the third view controller is not displayed at runtime but is displayed in IntefaceBuidler at design time.
This third view controller needs to be displayed modal.
What I'm doing wrong to display this bar item button?
A sample project illustrating the problem is available here.
Below a screen capture of the bar item button at design time:
Below a screen capture of the bar item button not showing at design time:
PS:
Please disregard the "Unknown class ThirdViewControlller in Interface Builder file.", since the ThirdViewController is displayed fine at runtime. Also, the "Done" button in the middle of the view works fine.
In SecondViewController you need to push the third onto the navigation controller stack like so:
self.navigationController?.pushViewController(thirdViewController, animated: true)
You are currently presenting it as a modal. Also, you've unnecessarily added a second UINavigationController to your storyboard (for the third view controller)
If you want to present a modal, then you'd need to embed the controller in a navigation controller:
let navController = UINavigationController(rootViewController: thirdViewController)
self.present(navController, animated: false)
If you prefer to keep this within the storyboard, then you need to provide a identifier for the UINavigationController and insatiate that in your function.
The above button is a navigation bar item that will only be displayed on the navigation bar . For achieving your desired result , you first have to embed the navigation controller at least in your second viewcontroller and then you should do a push segue rather than modal . Navigation controller can be added by
whith your second viewcontroller selected go to Editor\Embed In\Navigation Controller
for pushing the viewcontroller programatically onto user navigation controller's stack use
self.navigationController?.pushViewController(nextViewController, animated: true)

Keep a header view when using a navigation controller

I have a label I placed at the top of the screen as a header. I want to keep that viewable and still present when I add a navigation controller by embedding it.
I am mostly using the storyboard to help me create the UI.
This is what the app looks like without a navigation controller:
However, if I add a navigation controller I get the following:
Certainly I would like to maintain a navigation controller so that I won't have to incorporate my own buttons which control navigation. As I understand it, at this time the Marketplace logo is hidden behind the nav controller.
You can use two methods here :
1) Keep using your label, and hide the navigation bar , your app will still have navigation, just the top navigation bar will be hidden.
For this, use this code:
viewDidLoad()
{
self.navigationController?.navigationBar.isHidden = true
}
2) The second method is a alternate to what you want, but its good if you Need text At top only. In this we will not use label, instead we'll set title of the navigation bar itself.
viewDidLoad()
{
self.navigationItem.title = "MarketPlace"
}
Maybe there are more methods, but these two i've used.
You can try navigationItem.titleView = yourView
In Objective-C it would be something like this:
navigationController.navigationBar.hidden = YES;

Unable Correctly to Add Navigation Buttons Using UIPageViewController for iOS

For my project UIPageViewController could be a good choice, so, I started with the template provided by XCode 6.4 starting new project. I used the template: Page-Based Application which has already the basic implementation of UIPageViewController. The opportunity to scroll the pages is working fine, however, I would like to add the navigation bar as well. After reading the articles on that topic I tried to embed DataViewController into NavigationController (Editor>Embed), and then added the Navigation Bar Button using the storyboard from Interface Builder. However, I cannot see that button when I compile the app. Moreover, it looks that the back navigation works for the pages when you press on the spot where Navigation Bar Button should be located, but button is not visible. Later I found out that the back navigation works not only on that spot but on all left margin of the view. It seems like a bug.
1. How to make Navigation Bar Back Button visible?
2. How to remove that buggy behavior when navigation reacts to the press of all the margin instead of just location of the button?
If you want to have the navigation controller show up, you need to change:
let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!
so that startingViewController is the navigation controller your dataViewController is embedded in. You can do this by changing the storyboard ID of the navigation controller and saying something like:
let startingViewController: UIViewController = (self.storyboard?.instantiateViewControllerWithIdentifier("NavController"))!
However this is going to break the template as the rest of the code thinks the starting view controller is a DataViewController, so you will need to change that.
The "buggy behavior" is the standard page controller navigation mechanism when the transitionStyle is set to UIPageViewControllerTransitionStylePageCurl.

Programming Transition Loses Navigation Bar

I'm having an issue with my main.storyboard file. I changed the settings of my app so that the start screen is the main.storyboard file, rather than LaunchScreen.xib. The initial ViewController is the NavigationController, and the second is my SplashScreeViewController. (I created my own splash screen in the storyboard so that I could change it with additional code.) I use a line of code in my splash screen to later transition to the second view controller. Here it is:
var controller:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Second") as! ViewController
controller.modalTransitionStyle = .CrossDissolve
self.presentViewController(controller, animated: true, completion: nil)
For some reason, when I transition to that second ViewController, the navigation bar that should be at the top (of the second ViewController) isn't there, opposite of what was shown in the main.storyboard file. I tried to add an invisible, disabled button to the splash screen as to add a connection between the two view controllers, and therefore adding a navigation bar to the second, but when the splash transitions on its own, no navigation bar appears on the second.
Is there a way I could have a navigation bar on my second view controller without dragging one in, nor programming it in? I would like to use the one Xcode provides when you create a new connection between controllers.
Thanks in advance to all who reply.
*(I apologize for the lack of pictures to help describe my problem. I don't have enough 'reputation' to do so.)
You need to embed your Second view controller on a Navigation Controller, and then instantiate that navigation controller instead.
This might solve the problem - It looks like you are presenting a modal view controller when you probably want to be pushing your view controller from your initial navigation controller with pushViewController

IOS Navigation Bar in Master/Detail storyboard project is transparent on ipad but not on iphone

I'm working on an IOS app that was started with the default Master/Detail application storyboard.. I haven't changed the original layout except for to add an additional segue from the Detail View controller to a new UIViewcontroller which I'm representing with my own class. I'd like the navigation Bar to be transparent on the view controller that I added and I've found lots of examples on how to make that happen on the internet. The problem I'm having is that when tested on an ipad, the naviagation bar is transparent as expected, but on and Iphone no matter what I do the navigation bar stays the same.
I'm running the Xcode 7 beta and everything is coded in Swift.
In my viewDidLoad function for my View Controller I have the following lines of code:
self.navigationController!.navigationBar.translucent = true;
self.navigationController!.navigationBar.shadowImage = UIImage()
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController.navigationBar.backgroundColor = UIColor.ClearColor()
I've tried these lines in a bunch of different ways... here are some of the things I've tried
running these lines in viewWillAppear instead.
Running the same basic code on the object returned from UINavigationBar.appearance()
getting the parent view controller and running the same settings on its navigationbar object.
None of these changes have had any effect on behavior. It appears that any IOS device where the Master View Controller displays first full-screen leave the navigation bar normal.. while any device that displays the details view or both views first shows the navigation bar transparent. I'm thinking I need to access the navigation bar in a different way in those scenarios.. but I'm out of ideas.
Thanks!
put
let navigationController = splitViewController!.viewControllers[splitViewController!.viewControllers.count-1] as! UINavigationController
on top of your code then change the self.navigationController! with navigationController and it will work.

Resources