ViewControllers inside UIScrollView not Visible swift - swift

I'm trying to add ViewControllers in UIScrollView and VC's are loaded from xib. This is my code
let onevc = OneViewController()
self.addChildViewController(onevc )
self.scrollView.addSubview(onevc .view)
onevc.didMoveToParentViewController(self)
let twovc = TwoViewController()
var frame:CGRect = twovc.view.frame
frame.origin.x = 320
twovc.view.frame = frame
self.addChildViewController(twovc)
self.scrollView.addSubview(twovc.view)
twovc.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(640, self.view.frame.size.height)
self.scrollView.pagingEnabled = true
Both the viewcontrollers are added but they are not visible, scrollview is just showing white screen. I have added few elements in ViewControllers and also to test I added println("something") So when the view is loaded, It is printing something but not showing anything. what am I doing wrong here?

Perhaps you forgot to specify the nib name for the view controller?
let onevc = self.storyboard!.instantiateViewControllerWithIdentifier("OneViewController")
or
let onevc = OneViewController(nibName: "OneViewController", bundle: nil)
Same goes for the 2nd one.

Related

NSPopover animate contentViewController change and change popover size

I am making a NSPopover, and have made it so I can transition between view controllers with a sliding animation using a parent view controller with my view controllers as children. It works well, except before I added this, the popover automatically resized to my view's size, but now, the popover is stuck at a fixed size.
The code for creating the popover:
self.homeVC = PopoverViewController(nibName: "PopoverViewController", bundle: nil)
self.loginVC = SignInViewController(nibName: "SignInViewController", bundle: nil)
self.containerView.view.wantsLayer = true
self.containerView.view.frame = self.homeVC!.view.bounds
self.containerView.addChildViewController(self.homeVC!)
self.containerView.view.addSubview(self.homeVC!.view)
popover.contentViewController = self.containerView
The code for transitioning the view controllers:
self.loginVC!.view.frame = self.homeVC!.view.bounds
self.containerView.addChildViewController(self.loginVC!)
let transition: NSViewControllerTransitionOptions = .SlideLeft
self.containerView.transitionFromViewController(self.homeVC!, toViewController: self.loginVC!, options: transition, completionHandler: { finished in
self.homeVC!.view.removeFromSuperview()
self.homeVC!.removeFromParentViewController()
self.containerView.view.bounds = self.loginVC!.view.bounds
})
Is there anyway that I can make the popover automatically resize to the size it is supposed to be after transitioning?
Thanks in advance.
I was able to fix it by instead of explicitly setting the frame, setting constraints to make the container view match the child's size.

Swift. How to add a UIView on top of a GMSPanoramaView

I have a simple GMSPanoramaView and I want to add a UIView on top of it, I've tryed whith addSubview and changing the layers zposition but I't dosen't show up.
Any idea's on how I can do it?
The way I implemented the GMSPanoramaView:
let panoView = GMSPanoramaView(frame: CGRectZero)
self.view = panoView
panoView.moveNearCoordinate(CLLocationCoordinate2DMake(latitude, longitude))
Try this:
Approach 1:
var yourView = UIView()
self.view.bringSubviewToFront(yourView)
Approach 2:
var gmsView = GMSPanoramaView()
self.view.addSubview(gmsView)
var anotherView = UIView()
self.view.insertSubview(anotherView, aboveSubview: gmsView)
This should work!!

How do i get rid of xib after presenting it?

I am creating my own UIImagePicker Overlay and am using a Xib in order to display the overlay. Whenever I present the imagepicker, it seems to present twice, once when I call the main bundle to load the nib and the other when I explicitly present the imagepickercontroller. The main problem i'm having is when I dismiss the imagepickercontroller, the first one goes, which is great, but the underlying view of the xib still remains and doesn't go regardless of what i do.
I tried to convert the Objective-C example project for how to create a custom uiimagepickercontroller Apple has on their website into Swift. I'm not sure what I"m doing wrong, here is my code:
func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
let picker = UIImagePickerController()
picker.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
picker.sourceType = sourceType
picker.delegate = self
if sourceType == UIImagePickerControllerSourceType.Camera {
//gets rid of the out of the box imagepicker
picker.showsCameraControls = false
//loads the overlay xib for the camera
NSBundle.mainBundle().loadNibNamed("OverlayView", owner: self, options: nil)
self.overlayView.frame = (picker.cameraOverlayView?.frame)!
picker.cameraOverlayView = self.overlayView
self.overlayView = nil
//makes the image capture screen the full screen size
let screenSize: CGSize = UIScreen.mainScreen().bounds.size
let cameraAspectRatio: Float = 4.0 / 3.0
let imageWidth:Float = floorf(Float(screenSize.width) * cameraAspectRatio)
let scale: Float = ceilf((Float(screenSize.height + 120) / imageWidth) * 10.0) / 10.0
picker.cameraViewTransform = CGAffineTransformMakeScale(CGFloat(scale), CGFloat(scale))
}
self.imagePickerController = picker
self.presentViewController(self.imagePickerController, animated: true, completion: nil)
}
Here is the code for a button on the xib dismissing the view. How can i fix this? Because i'm printing out the array of view controllers on the navigation stack, and it only shows one.
#IBAction func goBack(sender: AnyObject) {
self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
It sounds like you have your view controller's view outlet connected to the view in your .xib file in addition to the overlayView outlet. This would probably cause what you're describing.

Swift - Accessing child label text from parent after addSubview

Hi have an app that adds multiple subviews (UIViews) to the parent using:
if let page:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("recentVC") as? RecentViewController {
page.view.frame = self.frame
scrollView.addSubview(page.view)
}
In RecentViewController I have a number of elements (labels, image views etc). I would like to set these from the parent view controller. Is this possible and if so any resources?
I have been searching for a while but I can only find info when segues are used.
Thanks
Since you are using storyboard you can create referencing outlets for views in RecentViewController by dragging them from storyboard to view controller class. Then you can access these properties directly as follow :
if let page = self.storyboard?.instantiateViewControllerWithIdentifier("recentVC") as? RecentViewController {
page.view.frame = self.frame
page.someChild.image = someImage // raw example
self.recentViewController = page
scrollView.addSubview(page.view)
}
You have most the work done for you, create a property in the parent view controller to keep the child view controller.
if let page = self.storyboard?.instantiateViewControllerWithIdentifier("recentVC") as? RecentViewController {
page.view.frame = self.frame
self.recentViewController = page
scrollView.addSubview(page.view)
}
Now you can access anything in the child view controller with self.recentViewController.<childControl>.
IMPORTANT NOTE: When setting up this kind of parent-child relationships, you should use the method from Creating Custom Container View Controllers
if let page = self.storyboard?.instantiateViewControllerWithIdentifier("recentVC") as? RecentViewController {
self.recentViewController = page
self.addChildViewController(page)
page.view.frame = self.frame
scrollView.addSubview(page.view)
page.didMoveToParentViewController(self)
}
I know this looks mysterious and unnecessary but it will prevent odd nearly untraceable issues in the future.
Oh, and if you ever need to remove the child view
self.recentViewController.willMoveToParentViewController(nil)
self.recentViewController.view.removeFromSuperview()
self.recentViewController.removeFromParentViewController()

Howto Size the Modal View programmatically (SWIFT)

I simply want to present a small option dialog over an existing main UIViewController/UIView , so that on an IPad I would see a small Dialog and in the Background I will see the Main View.
I managed to show a UIViewController/UIView in a modal view style as follow:
func showoptions(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Options") as! UIViewController
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
let popoverPresentationController = controller.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {
// set the view from which to pop up
_popoverPresentationController.sourceView = self.view;
//_popoverPresentationController.sourceRect = CGRectMake(60, 100, 500, 500)
//_popoverPresentationController. .setPopoverContentSize(CGSizeMake(550, 600), animated: true)
//_popoverPresentationController.sourceView.sizeToFit();
// present (id iPhone it is a modal automatic full screen)
self.presentViewController(controller, animated: true, completion: nil)
}
}
But I have still some issues:
1. Howto get rid of the arrow shown at the border.
2. Howto size this modal view. It is shown to small and I would like to fit it to the largest controls in the UIControllerView/UIView.
any help ?
Try changing _popoverPresentationController.permittedArrowDirections to empty option set
Change controller.preferredContentSize to match your desired size
I needed something similar and ended up presenting a view controller as a modal with a transparent background over the current context. There I made a smaller opaque UIView with what I wanted.