Tuesday, September 25, 2018

Whats different in iOS 10 for Today Extensions?

In iOS 8 and iOS 9, Show More/Show Less needed to be handled explicitly by the developers. It was a tedious task since it required constraint and layout handling to adjust the widget’s height according to the content. But in iOS 10, Apple provided APIs to handle such functionality without taking much pain.

NCWidgetDisplayMode
There exist 2 modes in which you can display data in your widget. These modes are categorized under NCWidgetDisplayMode as compact and expanded.
Compact mode has a fixed height of 110 and
Expanded mode is used for variable height according to your content.
Show More or Show Less buttons are shown in the widget’s top right corner according to the widget’s active display mode i.e. in compact mode, show more is visible and in expanded mode, show less is visible.

override func viewDidLoad()
    {
        super.viewDidLoad()
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    }

widgetLargestAvailableDisplayMode signifies the largest display mode supported by your app.
When set to compact, your app will only support compact mode i.e. show more/show less functionality will no longer be supported and your widget will have a fixed height of 110.
When set to expanded, the app will support both compact and expanded mode and show more/show less functionality will work accordingly.


Handling Height according to Display Mode
NCWidgetProviding protocol provides a delegate method widgetActiveDisplayModeDidChange(_: maxSize:) that handles the size of the widget in compact and expanded mode.
Width of the widget remains same according to the device you are using. There is no provision provided to change it.
Height of the widget can be changed according to the active display mode.

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize)
    {
        if activeDisplayMode == .expanded
        {
            preferredContentSize = CGSize(width: 0.0, height: 220)
        }
        else
        {
            preferredContentSize = maxSize
        }
    }

references:
https://hackernoon.com/app-extensions-and-today-extensions-widget-in-ios-10-e2d9fd9957a8

No comments:

Post a Comment