Albert Cortada
Albert Cortada

Reputation: 747

iOS Today Widget show more compact mode

I'm trying to show the today widget, with the button to Show more, but in iOS 10 I have only found info to show the button to Show More/less if I put the NCWidgetDisplayMode to Expanded. But then, the today widget is show expanded, and I would like it Compacted for the first time.

If I put the NCWidgetDisplayMode to Compact, then the Show more button disappears!

I have some apps that the behavior of the widget it's as I would like: the first time you set the widget, it's shown in compact mode, and with the button to Show more.

How can I archive this result? I have fond nothing that explains how to do it.

Upvotes: 0

Views: 1374

Answers (1)

PGDev
PGDev

Reputation: 24341

You need to set widgetLargestAvailableDisplayMode in viewDidLoad.

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

Now implement NCWidgetProviding protocol delegate method:

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize)
{
    if activeDisplayMode == .expanded
    {
        preferredContentSize = CGSize(width: 0.0, height: 200.0) //Size of the widget you want to show in expanded mode
    }
    else
    {
        preferredContentSize = maxSize
    }
}

For more on how widgets work in iOS8/iOS9/iOS10 you can refer to: https://github.com/pgpt10/Today-Widget

Upvotes: 3

Related Questions