Widgets, iOS 10, Xcode 7 and sizing
With the launch of iOS 8 Apple introduced Widgets. On iOS 9 not much changed and the usage of Widgets remained low for all our apps. Introducing iOS 10 Apple tries to put Widgets more in the spotlight.
One of the biggest (and potentially one that’s going to break all your widgets) changes is that Widgets now can have two modes: compact and expanded.
Compact mode has a fixed height of 110px (as far as I have seen) and for Expanded mode you can set your own preferred height like before.
Apple chose to implement the Compact mode by default (personally I think that was a bad choice since all your widgets which where developed with a height of more than 110px are just cut off).
Also you cannot fix this officially yet since Xcode 8 is still in beta and Apple does not accept builds made with a beta version…
So what to do? Well we can just try to work around it: call the functions, set the properties and see what happens on iOS 10.
For this we’ve created first an enum ‘copying’ the Apple NCWidgetDisplayMode
In the root widget view controller we added a few methods that allowed us to ‘expose’ the new enum. For iOS 9 and below we fallback to the Expanded mode since that mode behaves equal to the current widget behaviour.
This code by itself did not solve our problem. Our widget stayed in compact mode. It only allowed us to ‘prepare’ for iOS 10 and the migration to Swift 3.0.
To make the widget go into Expanded mode, iOS needed one more push (the following code is placed in the loadView method).
We built our app using this code and submitted it to TestFlight so we could run it on an iOS 10 device. And it worked! Well not a 100% as expected, the Widget was in expanded mode but did not display the show less button. This was not a loss for us since we really had use for the Expanded Mode only.
For a short time we tried to solve this issue by implementing the following method in the root widget view controller:
but that didn’t make a difference. After that we stopped, since we already achieved what we wanted.
By working this way our app still supports iOS 7 (unlike Xcode 8) and at the same time give our iOS 10 user the best experience possible.
Note: this only works on iPads, iPhones will default back to compact mode.