0

I'm trying to create an overlay on top of an ImagePicker, and finding I can create the UIView programmatically, but would prefer to use a UIView that I can manipulate within Storyboard.

As you can see from the code snipper below, I've assigned the UIImagePickerControllerDelegate to my ViewController, created the image picker, and have a method for assigning the overlay view that I want. If I try to reference a UIView that is an outlet tied to a UIView that exists in the storyboard, it doesn't appear. If If create it programmatically, all good. How can I assign it to the UIView that I want to drag-drop controls onto in Storyboard?

Thanks in advance for any advice/guidance.

Cheers, Carl

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {  
// UIView for telemetry data  
@IBOutlet weak var telemetryPanel: UIView!  

...  

override func viewDidAppear(animated:bool) {  
      ...  
      let cameraImagePicker = UIImagePickerController()  
      ...  

      // Call method to create a custom overlay for the camera  
      cameraImagePicker.cameraOverlayView = self.customViewForImagePicker(cameraImagePicker)  

 }  

func customViewForImagePicker(imagePicker: UIImagePickerController!) -> UIView {  
    // TODO: - Assigning the view to the telemetry panel does not work  
// let view:UIView = self.telemetryPanel  
// view.backgroundColor = UIColor.whiteColor()  
// view.alpha = 0.25  
// return view  

    // Creating the view programmatically overlays it  
    let cameraAspectRatio:CGFloat = 4.0 / 3.0;  
    let screenSize = UIScreen.mainScreen().bounds.size  
    let imageWidth = floorf(Float(screenSize.width) * Float(cameraAspectRatio))  
    let view: UIView = UIView(frame: CGRectMake(0, screenSize.height-65, CGFloat(imageWidth), 65))  
    view.backgroundColor = UIColor.whiteColor()  
    view.alpha = 0.25  
    return view  

}  
  • When you say storyboard, are you talking about using interface builder with xibs? What did you try when adding the overlay via xib? You've shown us code that works, but what about your code that doesn't work? – Beau Nouvelle Jun 29 '15 at 03:38
  • Thanks for the reply Beau, the code that isn't working is the code which is commented out, in the section "Assigning the view to the telemetry panel does not work". The telemetry panel is a UIView placed in a View Controller on my main storyboard. – cjlambre Jul 06 '15 at 03:16

1 Answers1

0

When initialising a view that has been created in interface builder, you will need to load it using NSBundle.mainBundle().loadNibNamed()

Check out this answer on how to do that.

Community
  • 1
  • 1
Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • Thanks for your help on this. I've got most everything in place, but not quite there. I followed the steps in the answer you linked, and have changed the code in my ViewController class (in a way I could not find to neatly display in this comment). The only remaining issue is an error on OverlayView stating "Missing argument for parameter 'coder' in call. What do I pass to satisfy the requirement in the init function for an NSCoder? – cjlambre Jul 06 '15 at 03:32
  • Did you end up getting this sorted? – Beau Nouvelle Oct 16 '15 at 04:52
  • No, I got distracted off onto another app that I wanted to write. Unfortunately, coding in Swift is not my day job, so I only get small bits of time here and there to work on these apps. Thanks for your input though, hoping to wrap up the app I'm working on soon and then hopefully get back to the one that generated this question. – cjlambre Oct 17 '15 at 04:57