iOS 8 Share Extension custom view controller size

Sami picture Sami · Nov 17, 2014 · Viewed 8.3k times · Source

I'm building a share extension for my iOS app and I can't use the default SLComposeServiceViewController, so I created (in the storyboard) a basic UIViewController and embedded in a navigation controller. I get to present it, dismiss it etc but it's always full screen. I would like to make it look more like a dialog. I have tried using self.preferredContentSize on my view controller, tried Use Preferred Explicit Size on the navigation controller in Interface Builder, but it doesn't work.

Answer

Dale picture Dale · May 22, 2016

This can be done easily and directly using the storyboard but is not immediately obvious. There is no need for multiple view controllers.

  1. Create your own view controller class inheriting from UIViewController.
  2. In the MainInterface.storyboard change the class of the view controller to your new class
  3. In the storyboard you can simply draw your UI - but here is the trick. You need to understand that the storyboard view will fill the screen and by default the view has already been created with a clear background. You simply need to create a view inside of the main view. You can set auto layout constraints to size this view and position it (e.g. centered horizontally and vertically). You can also use size classes to cause this inner view to fill the screen on compact layouts. Connect the controls from the inner view to your view controller in the usual way by control-dragging

Sample Storyboard

  1. In you custom view controller you can refer to self.extensionContext to read and complete the share action. Refer to the code in the template ShareViewExtension