Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 Share Extension custom view controller size

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.

like image 988
imas145 Avatar asked Nov 17 '14 18:11

imas145


2 Answers

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
like image 184
Dale Avatar answered Oct 19 '22 00:10

Dale


Ok I spent a long time trying to figure this out. For whatever reason you can't set the view size like you can with the action extension. It will always go full screen (even on iPad). So a way around this is to make multiple view controllers. Make the first view controller hidden so the user doesn't notice that there is a full screen view controller present (And yes, in a share extension the first hidden view controller actually hides completely so the user doesn't even know it was presented). Then present your actual view controller using the hidden view controller. This way you can present it any way you like and set the size etc.

In my case I actually made my hidden view controller have a UIEffectView so I can have a nice blur animation in the background and then present my actual view controller over it.

Here is a picture: enter image description here

like image 30
Epic Byte Avatar answered Oct 19 '22 00:10

Epic Byte