I want to scrolling view when navigationItem clicked.
But..here is some error.
I want to make snapchatlike scroll view.
It works fine, but... when I call scrollView out of viewdidload function, scrollView is nil!
I already make snapchat like swipe view. but I want to make swipe view when navigation Item(heart mark) clicked.
My ViewController viewdidload func - it works fine
#IBOutlet weak var scrollView: UIScrollView!
var V1 : LeftViewController = LeftViewController(nibName: "LeftViewController", bundle: nil)
var V2 : CenterViewController = CenterViewController(nibName: "CenterViewController", bundle: nil)
var V3 : RightViewController = RightViewController(nibName: "RightViewController", bundle: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.addChildViewController(V1)
self.scrollView.addSubview(V1.view)
V1.didMoveToParentViewController(self)
self.addChildViewController(V2)
self.scrollView.addSubview(V2.view)
V2.didMoveToParentViewController(self)
self.addChildViewController(V3)
self.scrollView.addSubview(V3.view)
V3.didMoveToParentViewController(self)
var V2Frame : CGRect = V2.view.frame
V2Frame.origin.x = self.view.frame.width
V2.view.frame = V2Frame
var V3Frame : CGRect = V3.view.frame
V3Frame.origin.x = 2 * self.view.frame.width
V3.view.frame = V3Frame
println(self)
println(scrollView)
println(V2.view.frame)
self.scrollView.setContentOffset(CGPointMake(V2Frame.origin.x, self.view.frame.size.height), animated: false)
self.scrollView.contentSize = CGSizeMake(self.view.frame.width * 3, self.view.frame.size.height) }
My ViewController clickEvent func - why scrollView is nil?
func clickEvent() {
self.scrollView?.scrollRectToVisible(V2.view.frame, animated: true)
println(self)
println(scrollView)
println(V2.view.frame)
//self.scrollView.setContentOffset(CGPointMake(V2.view.frame.origin.x, self.view.frame.size.height), animated: true)
}
I call clickEvent in Viewcontroller from other CenterViewController
- it can call fine but...in ViewController clickEvent functions' scrollView is nil.. How can I fix it?
func clickEvent(sender: AnyObject) {
ViewController().clickEvent()
}
in your code you get and set a view frame In viewDidLoad. In viewDidLoad views are only loaded in memory but not yet in screen, so if you try to ask a view for its frame in viewDidLoad you will Not get a consistante value..
Instead use viewWillAppear where you can ask view for its frame and you get a right value since the view is ready to be shown.
The class itself does not contain the layout information in it. So there is no way the view controller object know which view object is connected to which IBOutlet. The view controller have to be instantiated through storyboard itself. You can use the following methods
storyboard?.instantiateInitialViewController()
storyboard?.instantiateViewControllerWithIdentifier("YOUR_VC_STORYBOARD_ID")
When you use the second method don't forget to set the Storyboard Identifier.
You can find the place to enter the identifier when you go to View Controller -> Identity Inspector -> Storyboard_ID
Related
I have a view controller that contains a collectionView with 2 sections. The header of the second section is a sticky header and it has a segmentedControl inside of it:
ParentViewController
--collectionView
--sectionOne // because there is specific data in sectionOne I cannot use a PageViewController
--sectionTwo
sectionTwoHeader // sticky header
[RedVC, BlueVC, GreenVC] // these should be the size of sectionTwo
When a segment is selected I'm using a ContainerVC that will show a view controller corresponding to each segment:
// each of of these color vcs have collectionViews inside of them
RedCollectionViewController(), BlueCollectionViewController(), GreenCollectionViewController()
The problem is when the segment is selected the collectionView isn't showing any of the color view controllers it's supposed to show. How do I add each color vc using addChildViewController() to a collectionView?
The collectionView w/ segmentedControl's selectedIndex:
class ParentViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
var collectionView: UICollectionView!
var containerController: ContainerController!
var vc: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
containerController = ContainerController()
}
#objc func selectedIndex(_ sender: UISegmentedControl){
let index = sender.selectedSegmentIndex
switch index {
case 0:
containerController.vcIdentifierReceivedFromParent(segment: "BlueVC")
break
case 1:
containerController.vcIdentifierReceivedFromParent(segment: "RedVC")
break
case 2:
containerController.vcIdentifierReceivedFromParent(segment: "GreenVC")
break
default: break
}
/*
// because of the X and Y values this adds the containerVC over the collectionView instead of under the sectionTwo segmented Control header
vc = containerController
addChildViewController(vc)
vc.view.frame = CGRect(x: 0,y: 0, width: collectionView.frame.width,height: collectionView.frame.height)
view.addSubview(vc.view)
vc.didMove(toParentViewController: self)
lastViewController = vc
*/
}
}
ContainerVC:
class ContainerController: UIViewController {
var vc: UIViewController!
var lastViewController: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
vcIdentifierReceivedFromParent(segment: "RedVC")
}
func vcIdentifierReceivedFromParent(segment: String){
switch segment {
case "RedVC":
let redVC = RedCollectionViewController()
addVcToContainer(destination: redVC)
break
case "BlueVC":
let blueVC = BlueCollectionViewController()
addVcToContainer(destination: blueVC)
break
case "GreenVC":
let greenVC = GreenCollectionViewController()
addVcToContainer(destination: greenVC)
break
default: break
}
}
func addVcToContainer(destination: UIViewController) {
//Avoids creation of a stack of view controllers
if lastViewController != nil{
lastViewController.view.removeFromSuperview()
}
self.vc = destination
addChildViewController(vc)
vc.view.frame = CGRect(x: 0,y: 0, width: view.frame.width,height: view.frame.height)
view.addSubview(vc.view)
vc.didMove(toParentViewController: self)
lastViewController = vc
}
}
You are adding Red / Blue / Green VCs to Container View controller that is referenced from inside ParentViewController. But you are adding each of them inside ContainerVC topmost view, whose frame is probably never set, as far as I can see from your code.
It's probably CGRectZero.
Adding child VC views to this view will result in they are getting wrongly positioned, or not positioned at all. Because Container View controller is nowhere in the view controller hierarchy. You are effectively doing everything within ParentViewController's viewDidLoad(). Most probably, ContainerVC's viewDidLoad is not even called. Hence its view is never initialised properly.
You probably do not need ContainerVC at all. Try adding children to ParentViewController, and try adding them after viewDidLoad() call, i.e. in viewDidAppear(), viewDidLayoutSubviews() and upon switch segment selection.
I have a storyboard segue, and while showing the new view, I want an UIView to always stay on top, so the segue does not affect it. Tried animating insertSubview, but it does not have the push from bottom animation.
Here is some code I quickly whipped up showing two view controllers, the one you are going to and one from. I put the animation in a function called buttonPressed, but it should go whatever function that calls the transition. Rest of the code is pretty self explanatory.
For this to work both view controllers will need a view with same name (or different names but keep track of which is which), I use IBOutlet staticView. And then in interface builder make sure they have the same constraints or frame so that when you set on vc's static view to another it sticks to same spot.
class ViewControllerFrom: UIViewController {
#IBOutlet weak var staticView: UIView!
#IBAction func buttonPressed(_ sender: Any) {
let toVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "toVC") as! ViewControllerTo
//Add nextVC's view to ours as subview
self.view.addSubview(toVC.view)
//Set its starting height to be below current view.
toVC.view.frame = CGRect(x: 0, y: view.frame.height, width: view.frame.width, height: view.frame.height)
//This is important to make sure staticView stays in front of view animating in
view.bringSubview(toFront: staticView)
if toVC.staticView != nil {
toVC.staticView.isHidden = true
}
//I use animation duration 0.4, close to default animations by iOS.
UIView.animate(withDuration: 0.4, animations: {
toVC.view.frame.origin.y = 0
}, completion: { (success) in
if success {
//Now that view is in place, set static view from old vc to new vc and reshow it. Then do the actual presentation unanimated.
toVC.staticView.isHidden = false
toVC.staticView = self.staticView
self.present(toVC, animated: false, completion: nil)
}
})
}
}
class ViewControllerTo : UIViewController {
#IBOutlet weak var staticView: UIView!
}
I am dismissing a popover view controller programmatically. How can i detect that in my first view controller? Is there a way to send values from the popover to the first one?
Note: popoverPresentationControllerDidDismissPopover does not work when dismissed programmatically.
Any proposition?
this is my code in the main view controller:
let addFriendsPopoverViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeEmotionPopOver") as! EmotionPopOverViewController
addFriendsPopoverViewController.modalInPopover = true
addFriendsPopoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
addFriendsPopoverViewController.preferredContentSize = CGSizeMake(100, 100)
let popoverMenuViewController = addFriendsPopoverViewController.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .Any
popoverMenuViewController!.delegate = self
popoverMenuViewController!.sourceView = self.view
let height = (self.tableView.rowHeight - HeartAttributes.heartSize / 2.0 - 10) + (self.tableView.rowHeight * CGFloat((sender.view?.tag)!)) - 50
popoverMenuViewController!.sourceRect = CGRect(
x: 30,
y: height,
width: 1,
height: 1)
presentViewController(
addFriendsPopoverViewController,
animated: true,
completion: nil)
and in the popover view controller, i'm dismissing it from a button IBAction:
#IBAction func dismissPop(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
The way you have worded your question is that you are looking for a function on the main view controller that is called when a popover is dismissed.
This technically happens with viewDidAppear(animated:). However, it isn't a full proof solution. If your popover doesn't cover the full screen context, this function wont fire, so it is an unreliable solution.
Really what you want is to invoke a function from the popover alerting the main view controller that it has finished/dismissed. This is easily done with a delegate protocol
protocol PopoverDelegate {
func popoverDismissed()
}
class PopoverViewController {
weak var delegate: PopoverDelegate?
//Your Popover View Controller Code
}
Add the protocol conformance to your main view controller
class MainViewController: UIViewController, PopoverDelegate {
//Main View Controller code
}
Then you need to set the delegate to for the popover to be the main view controller.
let addFriendsPopoverViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeEmotionPopOver") as! EmotionPopOverViewController
addFriendsPopoverViewController.delegate = self
//The rest of your code
Finally, call this delegate function from your popover view controller when you dismiss.
#IBAction func dismissPop(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
delegate?.popoverDismissed()
}
And in your main view controller, implement the delegate method
func popoverDismissed() {
//Any code to run when popover is dismissed
}
The trick is to dismiss the segue yourself but make it seem that the user initiated it so it can be detected by the delegate method popoverPresentationControllerDidDismissPopover().
I did it by adding a completion closure to the presentingViewController dismiss() function and directly invoked the routine.
if let pvc = self.presentingViewController {
var didDismiss : ((UIPopoverPresentationController) -> Void)? = nil
if let delegate = popoverPresentationController?.delegate {
// check it is okay to dismiss the popover
let okayToDismiss = delegate.popoverPresentationControllerShouldDismissPopover?(popoverPresentationController!) ?? true
if okayToDismiss {
// create completion closure
didDismiss = delegate.popoverPresentationControllerDidDismissPopover
}
}
// use local var to avoid memory leaks
let ppc = popoverPresentationController
// dismiss popover with completion closure
pvc.dismiss(animated: true) {
didDismiss?(ppc!)
}
}
It is working fine for me.
EDIT
I think my problem is that I add the views as subviews in the same view, thats why I can't remove it ?
Im trying to learn swiping between views using XIB.
My storyboard contains 3 views
-Login
-Create Account
-View with scrollview that scrolls between a tableview and a blank view. This view has an embedded navigation controller (Editor -> Embed In -> Navigation Controller)
I Don't want the navigation controller to be shown in my blank page.
I have created the tableView Controller and the blank UIControllerView by adding them as "addChildViewController", See code below
import UIKit
class MasterViewForScroll: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
let Inbox : FriendlistTableBarView = FriendlistTableBarView(nibName: "FriendlistTableBarView", bundle: nil)
let Camera : CameraViewController = CameraViewController(nibName: "CameraViewController", bundle: nil)
func creatingSubViews() {
self.addChildViewController(Inbox)
self.scrollView.addSubview(Inbox.view)
Inbox.didMoveToParentViewController(self)
Inbox.navigationController?.navigationBar.hidden = false
var CameraView = Camera.view.frame
CameraView.origin.x = self.view.frame.width
Camera.view.frame = CameraView
self.addChildViewController(Camera)
self.scrollView.addSubview(Camera.view)
Camera.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.width * 2, self.view.frame.height)
}
override func viewDidLoad() {
super.viewDidLoad()
creatingSubViews()
}
So my question is: How do I hide the navigation controller in the "Camera" view.
Thank you
In your CameraViewController class, add the following code:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
}
Next, in your FriendlistTableBarView class (I guess it is a UIViewController subclass), add the following code:
override func viewWillAppear() {
super.viewWillAppear()
self.navigationController?.navigationBarHidden = false
}
So, when you swipe to the right - navigation bar will hide, and when you swipe to the left - it will appear again.
I have found few posts for this problem but none of them solved my issue.
Say like I've..
ViewControllerA
ViewControllerB
I tried to add ViewControllerB as a subview in ViewControllerA but, it's throwing an error like "fatal error: unexpectedly found nil while unwrapping an Optional value".
Below is the code...
ViewControllerA
var testVC: ViewControllerB = ViewControllerB();
override func viewDidLoad()
{
super.viewDidLoad()
self.testVC.view.frame = CGRectMake(0, 0, 350, 450);
self.view.addSubview(testVC.view);
// Do any additional setup after loading the view.
}
ViewControllerB is just a simple screen with a label in it.
ViewControllerB
#IBOutlet weak var test: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
test.text = "Success" // Throws ERROR here "fatal error: unexpectedly found nil while unwrapping an Optional value"
}
EDIT
With the suggested solution from the user answers, ViewControllerB in ViewControllerA is going off the screen. Grey border is the frame I have created for the subview.
A couple of observations:
When you instantiate the second view controller, you are calling ViewControllerB(). If that view controller programmatically creates its view (which is unusual) that would be fine. But the presence of the IBOutlet suggests that this second view controller's scene was defined in Interface Builder, but by calling ViewControllerB(), you are not giving the storyboard a chance to instantiate that scene and hook up all the outlets. Thus the implicitly unwrapped UILabel is nil, resulting in your error message.
Instead, you want to give your destination view controller a "storyboard id" in Interface Builder and then you can use instantiateViewController(withIdentifier:) to instantiate it (and hook up all of the IB outlets). In Swift 3:
let controller = storyboard!.instantiateViewController(withIdentifier: "scene storyboard id")
You can now access this controller's view.
But if you really want to do addSubview (i.e. you're not transitioning to the next scene), then you are engaging in a practice called "view controller containment". You do not just want to simply addSubview. You want to do some additional container view controller calls, e.g.:
let controller = storyboard!.instantiateViewController(withIdentifier: "scene storyboard id")
addChild(controller)
controller.view.frame = ... // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
view.addSubview(controller.view)
controller.didMove(toParent: self)
For more information about why this addChild (previously called addChildViewController) and didMove(toParent:) (previously called didMove(toParentViewController:)) are necessary, see WWDC 2011 video #102 - Implementing UIViewController Containment. In short, you need to ensure that your view controller hierarchy stays in sync with your view hierarchy, and these calls to addChild and didMove(toParent:) ensure this is the case.
Also see Creating Custom Container View Controllers in the View Controller Programming Guide.
By the way, the above illustrates how to do this programmatically. It is actually much easier if you use the "container view" in Interface Builder.
Then you don't have to worry about any of these containment-related calls, and Interface Builder will take care of it for you.
For Swift 2 implementation, see previous revision of this answer.
Thanks to Rob.
Adding detailed syntax for your second observation :
let controller:MyView = self.storyboard!.instantiateViewControllerWithIdentifier("MyView") as! MyView
controller.ANYPROPERTY=THEVALUE // If you want to pass value
controller.view.frame = self.view.bounds
self.view.addSubview(controller.view)
self.addChildViewController(controller)
controller.didMoveToParentViewController(self)
And to remove the viewcontroller :
self.willMoveToParentViewController(nil)
self.view.removeFromSuperview()
self.removeFromParentViewController()
This code will work for Swift 4.2.
let controller = self.storyboard!.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
controller.view.frame = self.view.bounds
self.view.addSubview(controller.view)
self.addChild(controller)
controller.didMove(toParent: self)
For Add and Remove ViewController
var secondViewController :SecondViewController?
// Adding
func add_ViewController() {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController")as! SecondViewController
controller.view.frame = self.view.bounds
self.view.addSubview(controller.view)
self.addChild(controller)
controller.didMove(toParent: self)
self.secondViewController = controller
}
// Removing
func remove_ViewController(secondViewController:SecondViewController?) {
if secondViewController != nil {
if self.view.subviews.contains(secondViewController!.view) {
secondViewController!.view.removeFromSuperview()
}
}
}
Thanks to Rob, Updated Swift 4.2 syntax
let controller:WalletView = self.storyboard!.instantiateViewController(withIdentifier: "MyView") as! WalletView
controller.view.frame = self.view.bounds
self.view.addSubview(controller.view)
self.addChild(controller)
controller.didMove(toParent: self)
func callForMenuView()
{
if(!isOpen)
{
isOpen = true
let menuVC : MenuViewController = self.storyboard!.instantiateViewController(withIdentifier: "menu") as! MenuViewController
self.view.addSubview(menuVC.view)
self.addChildViewController(menuVC)
menuVC.view.layoutIfNeeded()
menuVC.view.frame=CGRect(x: 0 - UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width-90, height: UIScreen.main.bounds.size.height);
UIView.animate(withDuration: 0.3, animations: { () -> Void in
menuVC.view.frame=CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width-90, height: UIScreen.main.bounds.size.height);
}, completion:nil)
}else if(isOpen)
{
isOpen = false
let viewMenuBack : UIView = view.subviews.last!
UIView.animate(withDuration: 0.3, animations: { () -> Void in
var frameMenu : CGRect = viewMenuBack.frame
frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width
viewMenuBack.frame = frameMenu
viewMenuBack.layoutIfNeeded()
viewMenuBack.backgroundColor = UIColor.clear
}, completion: { (finished) -> Void in
viewMenuBack.removeFromSuperview()
})
}
Please also check the official documentation on implementing a custom container view controller:
https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html#//apple_ref/doc/uid/TP40007457-CH11-SW1
This documentation has much more detailed information for every instruction and also describes how to do add transitions.
Translated to Swift 3:
func cycleFromViewController(oldVC: UIViewController,
newVC: UIViewController) {
// Prepare the two view controllers for the change.
oldVC.willMove(toParentViewController: nil)
addChildViewController(newVC)
// Get the start frame of the new view controller and the end frame
// for the old view controller. Both rectangles are offscreen.r
newVC.view.frame = view.frame.offsetBy(dx: view.frame.width, dy: 0)
let endFrame = view.frame.offsetBy(dx: -view.frame.width, dy: 0)
// Queue up the transition animation.
self.transition(from: oldVC, to: newVC, duration: 0.25, animations: {
newVC.view.frame = oldVC.view.frame
oldVC.view.frame = endFrame
}) { (_: Bool) in
oldVC.removeFromParentViewController()
newVC.didMove(toParentViewController: self)
}
}
Swift 5.1
To Add:
let controller = storyboard?.instantiateViewController(withIdentifier: "MyViewControllerId")
addChild(controller!)
controller!.view.frame = self.containerView.bounds
self.containerView.addSubview((controller?.view)!)
controller?.didMove(toParent: self)
To remove:
self.containerView.subviews.forEach({$0.removeFromSuperview()})