how to block presses around child view controller using swift 4.0? - ios

i'm attaching a child view controller to my view :
func showRaitingDialog () {
let popupRateExpiriance = UIStoryboard (name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ratePopupId") as! RateViewController
//adding the popup view to the corrent view controller
print ("this happens after")
self.addChildViewController(popupRateExpiriance)
popupRateExpiriance.refrenceToController = self
popupRateExpiriance.view.frame = self.view.frame
self.view.addSubview(popupRateExpiriance.view)
popupRateExpiriance.didMove(toParentViewController: self)
}
that child view controller is actually just a costume dialog that i did in my app.
my question is how do i block the presses around this dialog ? (so far the user can press on the screen blow it if he presses around the view controller itself)

You need to build a custom component and introduce the padding between the newly build child and your current one.
#IBAction func buttonAction(_ sender: Any) {
showRaitingDialog ()
}
func showRaitingDialog () {
let popupRateExpiriance = UIStoryboard (name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ratePopupId") as! RateViewController
//adding the popup view to the corrent view controller
addChild(popupRateExpiriance)
// popupRateExpiriance.refrenceToController = self // << maybe you should create an interface / delegate
popupRateExpiriance.view.frame = self.view.frame
view.addSubview(popupRateExpiriance.view)
popupRateExpiriance.didMove(toParent: self)
}
The controller on the right has the background view colour set to clear.
Here is the result at runtime after the button has been pressed to display the child controller (the user can no longer press the button).

Related

Pass Values from Popup VC to controller

Currently in my App I am opening a popup View Controller inside a normal View Controller, and need to pass values back from my Popup VC to the normal VC.
This is how I am making the popups.
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IconEditPopup") as! IconEditPopup
// this is where you can set values in the view
vc.id = "12"
self.addChild(vc)
vc.view.frame = self.view.frame
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
Basically just want to pass values back from the popup View controller to my normal VC.
You can use Delegate pattern or Clouser callback handler to pass a value back to the parent view.
here is a example:
Define a clouser in your popVC like this:
var clouserName: ((returnType) -> Void)?
Inside your popVC where you need to call the clouser:
clouserName?(returnValue)
in your parent controller, capture the value in this way:
vc.clouserName = { returnValue in // dont forget [weak self] if you need self
// Do your stuff here
}

Is there a way to connect or access my `scrollView`from my main class to another class?

in my MainViewController I have a scrollView and add a ChildView in it which works fine. In this example I instantiate my PlansViewController2and add it as a ChildViewto my scrollView.
After selecting a Cellit pushes a new controller. But I want to add the new Controller as a ChildView to my `scrollView.
#IBAction func planAction(_ sender: UIButton) {
let newPlan = sender.frame.origin.x
scrollView.subviews.forEach{$0.removeFromSuperview()}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextController = storyBoard.instantiateViewController(withIdentifier: "PlansViewController2") as! PlansViewController2
addChildView(viewController: nextController, in: scrollView)
self.movingView.frame.origin.x = newPlan
}
Im my PlansViewController2 I push another ViewControllerbut I want to add it also in my scrollView from the previous controller.
let storyBoard = UIStoryboard(name:"Main", bundle:nil)
let planViewCtrl = storyBoard.instantiateViewController(withIdentifier: "PlanViewCtrl") as! PlanViewController
planViewCtrl.navigationItem.title = plan.title
self.show(planViewCtrl, sender: self)
_ = planViewCtrl.view // force view loading, so we can set it up properly
_ = self.updateLastUsed(plan: plan)
planViewCtrl.plan = plan
Is it possible to add - in my case here - the PlanViewController into my scrollViewfrom my MainViewController?
When you add view's PlansViewController2 to scrollview. Please add PlansViewController2 to childViewController of VC has scrollView(self.addChild(nextController)).
When you want to add PlanViewController to scrollView:
Step 1: First you need to get viewcontroller that have scrollView using parent property (self.parent).
Step 2: Final, You have access to scrollView instance though vc you get in step 1.
Good luck!

remove navigation controller from superview

So i have an app were you're supposed to chose a location from a map but the way i'm doing it is by a popup off a view controller that has a MapKit and a search bar and a button for choosing users location, the problem lies on when i'm done with the View I normally called the
self.view.removeFromSuperview()
but the thing that is being remove is the View controller itself but il leaves behind the navigation bar and it doesn't let me do nothing else (in fact when I go to another tab and return to the same one the VC returns).
the way i'm instantiating the VC is like this:
func location ()
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "mapV") as! UBICACIONViewController
let nav1 = UINavigationController()
nav1.viewControllers = [vc]
self.addChildViewController(nav1)
self.view.addSubview(nav1.view)
vc.delegate = self //this for the info that i'm getting afte(works fine)
nav1.didMove(toParentViewController: self)
}
So until here works fin but when the user pick te button that says "Use My Location" and tries to return there's the problem.
in my VC view controller this is what happens when finished:
func Remove()
{
if let del = delegate {
del.dataChanged(str: Event.sharedInstance.Location!)
}
self.view.removeFromSuperview() //Problem Here :/
}
Second VC
Return to First VC
Actually you have added a child view controller nav1. And nav1 has a subview nav1.view.
while removing you are removing this view from its superview, not the child view controller added.
Any ways, From you screen short..I understand that the pop up view is off full screen then why dont you present this VC modally, or else push it to navigation stack.

Custom segue to sibling view controller

I have 3 pages that should segue like so...
,-(1)-> Home <-(2)-,
v v
Login ----(3)---> Register
where Login -> Register segue is triggered by a "Don't have an account? Register now." button, you know, something like that... Also, mind the unidirectional arrow.
All segues are already implemented, except for one: when a segue with this order is triggered:
Home -> Login -> Register -> Home
where Register -> Home is triggered via back button.
Basically, I need to know how to set Register's parentViewController to Home so that when the Back button is pressed (which then calls performSegueWithIdentifier with my custom unwind segue), Register would then unwind to Home.
Based on the segue order chain mentioned above, Register unwinds to a dismissed Login, I believe.
P.S. I'm using the interface builder for the views.
P.S. Login and Register are presented MODALLY, so they go on top of Home
To add custom animation to present view controller using custom animation
You have call self. performAnimation() function on your back button of Registration and Login. The below code is for Registration and Home screen.
// This function will be present in your Registration viewController
func performAnimation() {
//Your storyboard name
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let YourNavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("YourNavigationController") as! UINavigationController
let mainVC = mainStoryboard.instantiateViewControllerWithIdentifier("YourHomeScreenIdentifier") as! YourHomeScreen
initialNavigationController.viewControllers = [mainVC]
self.presentViewController(YourNavigationController, animated: false, completion: nil)
(mainVC as YourHomeScreen).fromRegisterSelector()
}
//These function or line of code should be present in your Home viewController
func fromRegisterSelector()
{
hasComeHereFromRegVC = true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if hasComeHereFromRegVC
{
addSignInViewAndAnimateIt()
}
}
func addSignInViewAndAnimateIt()
{
hasComeHereFromRegVC = false
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let mainNavigationController = mainStoryBoard.instantiateViewControllerWithIdentifier("YourNavigationController") as! UINavigationController
let RegVC = mainStoryBoard.instantiateViewControllerWithIdentifier("RegViewController") as! YourRegViewController
mainNavigationController.viewControllers = [RegVC]
self.view.addSubview(mainNavigationController.view)
self.view.bringSubviewToFront(mainNavigationController.view)
UIView.animateWithDuration(0.3, animations: {
// To change the screen sliding direction change the origin.x to origin.y and frame.width to frame.height in the line below
mainNavigationController.view.frame.origin.x += self.view.frame.width
}) { (finished) -> Void in
mainNavigationController.view.removeFromSuperview()
}
}
Hope this will help you.

transitioning from one view controller to another using a button

So, I created a login page with basic logic. If username and/or password are empty, it displays an error message. If they're full, then it transitions to the next page (home). I created a second ViewController on the storyboard, and created a SecondViewController". I then defined the respective ViewController class to "SecondViewController".
On the first ViewController.swift file, i used this code:
func transition(Sender:UIButton!)
{
let secondViewController: SecondViewController = SecondViewController()
self.presentViewController(secondViewController, animated:true, completion:nil)
}
When I tested it out however, pressing the login button (when both the username and password are filled) transfers me to a black screen instead of the second View Controller. Any ideas on what to do?
If you want the view to contain things added to it on the storyboard, you could give it a Storyboard ID (a couple of boxes below where you set the class of the view controller in the storyboard).
If you give it for example the ID "secondVC" you can then create it using the following:
let secondViewController = storyboard?.instantiateViewControllerWithIdentifier("secondVC") as? SecondViewController
You can't initialize a view controller with just the default init method.
You should probably create a view controller scene in your storyboard, add a unique identifier on it, instantiate it using instantiateViewControllerWithIdentifier:, and then display it like you are doing.
You'll want to make a segue between the view controllers in your Storyboard, and call self.performSegueWithIdentifier().
Or you can give your `UIViewController an identifier and present it programmatically too:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let secondViewController storyboard.instantiateViewControllerWithIdentifier("") as? SecondViewController
if let secondViewController = secondViewController {
self.presentViewController(secondViewController, animated:true, completion:nil)
}
There are basically three ways to do this
1)Creating segue in storyboard,though it will only work if you have single segue from that item
2)using prepareforSegue and performSegue Methods in your presenting view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourIdentifier" {
let controller = segue.destinationViewController as!secondViewController
}
}
Then use this in your IBACtion
performSegueWithIdentifier("YourIdentifier", sender:sender)
3)(Recommended One)
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ContactsVC")! as! ContactsVC
self.presentViewController(controller, animated:true, completion:nil

Resources