I was wondering if i could make some sort of a segue between these 2 views in a Xib file.
the main View is loaded into a scrollview in the storyboard.
.
(so if ik click Bewerken(edit) i would get pushed to the view controller on the right)
Thanks!
I would suggest changing this to a Container View inside the storyboard instead of a separated Xib. You can add and position/size UIContainerView as a subview and add a"Storyboard Embed Segue to attach another ViewController. This is what it will look like in IB/Storyboard:
During runtime, the blue UIView (or the embedded UIViewController if you like) will be embedded in the hosting UIView as a subview:
If you change your implementation to this, you are in the beautiful segue world where you can just drag-and-drop segues :)
You could do 2 things:
1 - Wrap the main view in a Navigation Controller so you can do the following:
#IBAction func loadEditController(sender:UIButton){
let editController = RegisterController(nibName:"RegisterXIB", bundle:nil)
navigationController?.pushViewController(editController, animated: true)
}
2 - Present the Edit Controller over the current context and animate it yourself
#IBAction func loadEditController(sender:UIButton){
let editController = RegisterController(nibName:"RegisterXIB", bundle:nil)
editController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
presentViewController(editController, animated: false, completion: nil)
//Move it offscreen and the animate it here
}
Related
I have the following storyboards:
"Home" is the default view controller. When you press the button in the top left with three lines, the menu view controller slides out (it's like a side menu). Within the menu there are four table cells that represent menu items, as you can see. When a cell is pressed, I have a corresponding function that is called. I want the view controller on the far right to be presented when a cell is pressed.
Here's the issue: I want the far right view controller to inherit the properties of the Home view controller, such that the navigation title and button are still there. How can I achieve this?
Here's the solution:
Make sure that the navigation and right view controller are segued (Ctrl + Drag in Interface Builder)
Call the code stated in #bebzerk answer:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let transition = storyboard.instantiateViewController(withIdentifier: "RightViewController") as! RightViewController
navigationController?.pushViewController(transition, animated: true)
Now go back to Interface builder, and add a UINavigationItem and a UIBarButtonItem to the view controller. Set the image of the button to the three lines and set the title of the navigation item to the name you want displayed on the top.
In the ViewController Swift file for the right view controller, Ctrl + Drag the bar button item and create an IBAction function. This will be called when the menu button (on the far right view controller is pressed). For me, this class extends from HomeViewController, so in the function, just called the super method. It should look like this:
#IBAction override func menuTapped(_ sender: UIBarButtonItem) {
super.menuTapped(UIBarButtonItem())
}
This should achieve the desired function.
Did you try this way ?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let transition = storyboard.instantiateViewController(withIdentifier: "RightViewController") as! RightViewController
navigationController?.pushViewController(transition, animated: true)
It should open your far right view controller and makes it inherit your home controller, like with a back button
Test it and tell me if it's good to you.
I am learning iOS with few sample projects. I have two view controllers in that first VC has few buttons and a mapview and the second VC has tableview showing a set of results. I have embed the both viewcontrollers in navigationViewController.By clicking a button from First VC i am able to show the tableview (using show segue) and able to go back to first VC through navigation. Now my query is I want to display the tableview (second VC) in place of one view object (map view) defined in firstVC rather than padding the tableview entirely in full screen. My problem is when showing another Viewcontroller i still want to see the few viewobjects from firstVC so I am trying to display the secondVC on top of mapview when i click on a button which triggers the segue.I have to use the single interface, so I need to load the tablview results from SecondVC into firstVC by replacing mapView's view with tableview.Please let me know your ideas if it is possible and any other ideas to achieve the same are most welcomed.
Sreekanth Gundlapalli,
All you need to do is to add the TableView controller's view as subview to your view Controller. In order to simplify the process I personally prefer using the ContainerView,
Step 1 : Add a ContainerView to your View Controller and add the auto layout constraints to it, because your tableView will be loaded inside this container view you donate have to apply any auto layout constraint to your tableView to keep it in place :)
ex :
Step 2 : Drag an IBOutlet to the container view.lets call it as containerView :)
Step 3 : Now its up to you to have two view controller 1 for loading map and 1 for loading tableView, or you will have map view as your view controller subview and you will either hide it or remove it and add container view but I personally prefer having code clean n neat so I prefer having two different VCs
so lets create 2 VCs lets call them as viewController1 & viewController2 Savy ??
Step 4 :
lets write a method which actually loads VC and adds its view as subview to your ViewController
func changeEmbededVC(for status : Int) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if status == 0 {
mehereButton.tag = 1
let vc = storyboard.instantiateViewController(withIdentifier: "viewController1")
vc.willMove(toParentViewController: self)
containerView.addSubview(vc.view)
self.addChildViewController(vc)
vc.didMove(toParentViewController: self)
}
else {
mehereButton.tag = 0
let vc = storyboard.instantiateViewController(withIdentifier: "viewController2")
vc.willMove(toParentViewController: self)
containerView.addSubview(vc.view)
self.addChildViewController(vc)
vc.didMove(toParentViewController: self)
}
}
I believe code is pretty self explanatory :D now what is mehereButton.tag = 1 ?? Simple you want to toggle view on button press don't you :D hence I have created a IBOutlet for mehereButton and changing its tag :)
now finally in IBAction of mehereButton
#IBAction func buttonTapped(_ sender: UIButton) {
self.changeEmbededVC(for: self.mehereButton.tag)
}
but we need to load one of the view by default isn't it :D
so change your viewDidAppear to
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.changeEmbededVC(for: 0)
}
Hope I answered your question In detail :P I know you can't neither up vote or accept answer as you don't have enough reputation :) but hope it will help somebody in future as well :)
I have just made a small transition so my project loads a different storyboard as its main (did it in the info.plist).
I have my new storyboard to keep my viewController that are responsible for login screen etc. Just to make it more clear.
After the login button is tapped I want to initiate a navigationController from another storyboard:
func instantiateViewController(fromStoryboard storyboard: String, withIdentifier identifier: String) -> UIViewController! {
let storyboard = UIStoryboard(name: storyboard, bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier(identifier)
return viewController
}
#IBAction func loginButtonTapped(sender: UIButton) {
let viewController = instantiateViewController(fromStoryboard: "Main", withIdentifier: "MainNavigationController")
presentViewController(viewController, animated: true, completion: nil)
}
Everything works correctly but one thing is driving me nuts.
After presenting the MainNavigationController from Main.storyboard its view hierarchy is not maintained.
What I mean is, the labels and buttons which supposed to be on top of another, full screen UIView (but are not its child and so they should remain) are now behind it.
What might be causing this and what is the simplest way to make them appear on top (as they do whey I open main.storyboard)
EDIT
I added a line of code in the rootView of the MainNavigationController in its viewDidLoad method:
self.view.sendSubviewToBack(wholeScreenView)
and it solved the problem.
However, does anybody know why do I have to code it myself and the views are not like in the Main.storyboard?
The storyboard is not configured the way you think it is. Your wholeScreenView is in fact in front of the other views, in the storyboard. The other views (the labels and buttons) are not subviews of wholeScreenView; they are subviews of the main view, and so is wholeScreenView. It is a later subview, so it is in front of them.
I create a view controller vc in storyboard and place a View1 on it and add all constraint to it. Now I want to add a vc as a subview. I use this code :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("LeftMenuListIdentifier")
self.view.addSubview(vc.view)
vc controller's add as subview to my current controller but it does not show any subview of vc controller'view means View1.
If I push vc controller to my current view controller then it shows View1.
self.navigationController?.pushViewController(vc, animated: true)
Please suggest what should I do . I want to add a view controller from storyboard as a subview.
Kindly Follow below steps for resolve your issue.
First Create UIViewController object.
after that Add object as a childviewcontroller as Like ( self.addChildViewController("Your Controller Object"))
After that add controller on self.view as add subview.( `self.view.addSubview("Your Controller Object"))
i think it will working
you don't need to push your vc, just add it to current viewcontroller using addChildViewController(vc);, this link may help you
I need to have a ViewController slide up, be centered, and overlay the current ViewController. I thought the following code would work but it does not:
let view = storyboard?.instantiateViewControllerWithIdentifier("castSpell") as! CastSpellViewController
view.modalPresentationStyle = .OverCurrentContext
presentViewController(view, animated: true, completion: nil)
The new ViewController is shown but just as a full screen view. I have it's size set to 400x300 in the storyboard. I know I can do the same thing by having a view within a view and then showing the overlay view. Is that the route I need to go?
You need to set the presentation style on the presenter, not the presenting view.
Use self.modalPresentationStyle = .OverCurrentContext
let popOverVC = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("ViewControllerName") as! PopOverViewContoller
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.containerView.frame
self.containerView.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(self)
Use the above code to add viewcontroller as a popover view. The container view is optional. I like using container views when the subviews position is fixed. I can position the container using autolayout and keep it hidden until needed.
In you case, you can place the container/subView at the bottom of the screen from where you need to slide up the subview.
UIView.animateWithDuration(0.5) { () -> Void in
//Animate here
}
Then change the frame of the container/subview inside the above block of code.
Note : Container view is just a normal UIView. I like naming it container :)