adding UIPageViewController to a view controller's container view - ios

I'm trying to Add my pageViewController to a container view on my main viewController . For that , I have added these codes
addChildViewController(pageViewController!)
self.sliderView.addSubview(pageViewController!.view)
pageViewController!.didMoveToParentViewController(self)
But when I run the app ,it's looks like this :
http://i.stack.imgur.com/0Sf17.jpg
I want to add my pageViewController in the yellow containerView which name is sliderView .
What's wrong with that?
Thanks

Please provide more code as the given example code works pretty well.
Possible causes could be the forced unwrap of your pageViewController. Additionally ensure that the controller is properly instantiated from Storyboard like
guard let vc = storyboard?.instantiateViewControllerWithIdentifier("YourViewControllerID") as? YourViewControllerClass else
{
return
}
sliderView.addSubview(vc.view)
vc.didMoveToParentViewController(self)
You can set YourViewControllerID directly within the Storyboards Identity Inspector

Related

TabBarController behind the scenes and correct approach for code behind

Just imagine( as case) that i have TabBarController as root and several ViewControllers that are linked to this TabBarController( via Storyboard(!) that is important step,i mean programmatically it's something other).
And its works without any code behind on TabBarController class!
So i'm interested in right approach, how can i Inistiate Viewcontrollers inside TabBarController on right way!?
Or it's isn't necessary and OS will do it for me , because at current moment, i can do something like this :
public override void ViewDidLoad()
{ //No Init for VC that are inside linked to TabBAr
base.ViewDidLoad();
//Find all viewcontrollers that are linked in storyboard
foreach (var Item in ViewControllers)
{
Item.TabBarItem.Image =
UIImage.FromBundle("RandomImg");
//Change title for all VC to "Main"
Item.TabBarItem.Title = "Main";
break;
}
}
What if i will do initialization of ViewControllers inside TabBarController constructor.
What will be happened on behind the scenes? How the system will recognize my init of VC,or it's doesn't matter and thats will be an redundant action, because technique via storyboard anyway creates ViewControllers without any initialization on code behind!?
How to handle/customize correctly ViewControllers that are inside of TabBarController?(e.g. select another VC at beginning, change buttons on TabBar and etc)
I would like to take all answers on all my questions and if it possible, need an live example/article that explains more advanced about this stuff. Thanks!

childViewControllers is empty after calling addChildViewController

I have a question regarding add/remove childviewcontrollers with ContainerView.
I have a UIViewController named "ContainerViewController" that contains a ContainerView, and this ContainerView has a UIViewController named "ContainerLinkViewController". I also have four other UIViewControllers that will be added to or removed from this "ContainerLinkViewController" whenever user a clicks one of the tab button that is placed at the bottom of "ContainerViewController". The storyboard looks like below.
NOTE that the custom segues connected to those four UiViewcontrollers are just empty segue that does nothing but show visual connection in storyboard.
So what I do is first add the First Tab ViewController to "ContainerLinkViewController" as a childViewController by using below code in viewDidLoad().
let firstTabViewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstTabViewController") as! FirstTabViewController
self.addChildViewController(firstTabViewController)
firstTabViewController.view.frame = self.view.bounds
self.view.addSubview(firstTabViewController.view)
firstTabViewController.didMove(toParentViewController: self)
After this initial setup, whenever user tabs on one of those tab buttons, I send signal from "ContainerViewController" that which UIViewController is to be added as new childViewController to "ContainerLinkViewController" and switching the old childViewController and this new childViewController using the below code.
func swapViewControllers(from : UIViewController, to : UIViewController) {
from.willMove(toParentViewController: nil)
self.addChildViewController(to)
to.view.frame = self.view.bounds
self.transition(from: from, to: to, duration: 1.0, options: UIViewAnimationOptions.transitionCrossDissolve, animations: nil) {
finished in
from.view.removeFromSuperview()
from.removeFromParentViewController()
to.didMove(toParentViewController: self)
}
}
Before calling this "swaptViewControllers()", I take the old childViewController by doing the following.
let oldChildViewController = self.childViewControllers.last
My problem is that this "oldChildViewController" is nil when I click one of those tabs for the first time right after the initial setup. So the function "swaptViewControllers()" does not even called because the self.childViewControllers is empty.
I have been struggling with this for few days now and could not find clear answer to solve this. So I decided to ask here to get some help about what might cause this.
Any help?? Thanks.
NOTE : I am using swift 3. Maybe this would help to answer my question since it seems lots of things changed fro swift 2.

storyboard views are not connected while using an iOS library

I am trying to implement a library(named: XLPagerTabStrip). Everything works fine with the help of this available tutorial [ https://github.com/xmartlabs/XLPagerTabStrip ].
My problem is: I can change my view controllers layout using code but i'm not able to change it through my storyboard; though the view controllers associated with the above library can be accessed. My storyboard views are well-connected with code.
Please help me get up from here. I'm stuck.
Solution :
Take the instance of the view controllers you wish to use inside MyPagerTabStripName( main controller class of library). In this way:
override internal func
viewControllersForPagerTabStrip(pagerTabStripController:
PagerTabStripViewController) -> [UIViewController] {
let firstVC = self.storyboard!.instantiateViewControllerWithIdentifier("myfirst") as! TabFirstViewController
let secVC = self.storyboard!.instantiateViewControllerWithIdentifier("mysecond") as! TabSecondTableViewController
return [firstVC, secVC]
}
Then you can access them using these identifiers in storyboard.

Set initial viewController in Navigation controller programmatically (Layer SDK)

I want to add Layer SDK to my application (using Swift).
All view controllers here are created programmatically. Therefore I can't segue to them. I have 4 tabs in my application (UITabBarController). One of them is chat. In the chat tab I created a segue to UINavigationController. Now I want to load conversationListViewController in this UINavigationController. For that I created a class for this UINavigationController i.e. ConversationListViewController and added the following code:
class ChatNavigationViewController: UINavigationController {
var conversationListViewController: ConversationListViewController!
var layerClient: LYRClient!
override func viewDidLoad() {
let appDelegate = UIApplication.sharedApplication().delegate as!AppDelegate
self.layerClient = appDelegate.layerClient
self.conversationListViewController = ConversationListViewController(layerClient: appDelegate.layerClient)
self.conversationListViewController.displaysAvatarItem = true
self.navigationController!.pushViewController(self.conversationListViewController, animated: true)
}
}
But this is not working. And giving this kind of effect: the ConversationViewController is not loaded in UINavigationController. I am not sure if I am doing it the correct way. I'm searching for the correct way, but unable to find.
I Solved it. I dragged new NavigationViewController and added ConversationListViewController to rootviewController.I think i should try this first. Anyways thanks guys for your help.
Because you want to do this programatically:
You need to manually initialize the controller before stacking it up on the Navigation Controller. Try this:
navigationController?.pushViewController(self.conversationListViewController.init(), animated: true)

Navigation Segue not Working properly iOS Swift

My segue is returning with an error (unwrapping an optional value) from this line > self.navigationController?....
In my code...
let nextScreen = self.storyboard!.instantiateViewControllerWithIdentifier("onboarding-what") as? ViewController
self.navigationController?.pushViewController(nextScreen!, animated: true)
This above code is implemented in my onboarding-start view controller
This is my storyboard set up...
the last view controller has the id of "onboarding-what".
I've read the documentation for the navigation controller but can't seem to find my problem. Does anyone have a solution?
You can use performSegueWithIdentifier rather than pushViewController if you connected them in storyboard
However did you fill "onboarding-what" in the storyboardId field?
pls check this image
Also "onboarding-what" view controller should be instance of ViewController
(There's not default class called "ViewController" only "UIViewController" or "NSViewController" for osX.)
Obviously nextScreen is nil. It is most likely that "onboarding-what" identifier is incorrect

Resources