2

Good evening!

I have big problems by understanding today extensions. I've read lot of tutorials and introductions but nothing helped me to understanding the problem. On iOS 9 the extension works fine - on iOS 10 not.

My big issue is the auto resizing(?) of the widget in iOS 10. On iOS 9 the widget show my table view in portrait and landscape perfect - iOS 10 crash that organization and break the view. The question on StackOverflow about that has only the answers like that: https://stackoverflow.com/a/38009125

But I won't display a button with "more" / "less". My extension should only display my tableView in a specific height, not more, not less.

The today extension that I use to test (I will build a similar extension with a table view if it works) is that: https://github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial (It's not my code - I'm happy about the work of the publisher!)

Here are two pictures of the problem (1. iOS 9, 2. iOS 10):

iOS 9: work

iOS 10: bug

I don't understand what is the problem. I set the height with self.preferredContentSize.height = 200 but iOS 10 dont use that height like iOS 9. And the StackOverflow answer above is not a solution because I won't a more / less button. Although I use the code of the more / less button the problem exist anymore in compact size:

// ViewDidLoad
if #available(iOSApplicationExtension 10.0, *) {
        self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded
    } else {
        self.preferredContentSize.height = 200
    }

// method in File
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
    if (activeDisplayMode == NCWidgetDisplayMode.compact) {
        self.preferredContentSize = maxSize
    }
    else {
        self.preferredContentSize = CGSize(width: maxSize.width, height: 200)
    }
}

As I said above - this code work only if the widget is pressed as "show more"... If the button is not pressed and the widget is in compact size it looks like the image above.

I hope anybody can help me because that is a problem I have for a really long time and I don't find enough material about today widgets on iOS 10.

Thanks!

Community
  • 1
  • 1

2 Answers2

6

Yes i had same problem. I solved it with widgetActiveDisplayModeDidChange and I call it in viewDidLoad
Swift 3

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
    self.preferredContentSize = (activeDisplayMode == .expanded) ? CGSize(width: 500, height: 230) : CGSize(width: maxSize.width, height: 110)
}

override func viewDidLoad() {
    super.viewDidLoad()

     self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded

    widgetActiveDisplayModeDidChange(.compact, withMaximumSize: CGSize(width: 500, height: 230))

}

enter image description here enter image description here

Rob
  • 2,649
  • 3
  • 27
  • 34
2

That's the iOS 10 behavior for today extensions. It takes a fixed height, same for all widgets, if you want to show more than that, you must have that more button. Also you can set the default display of your widget (extended or compact) : https://developer.apple.com/reference/notificationcenter/ncwidgetdisplaymode

Damien
  • 3,322
  • 3
  • 19
  • 29
  • Thanks for your answer! I have two questions about that: 1. How can I get the height of the extension as double value? So I could delete rows of the table view if the space is not enough... 2. On my private device I have a lot of extensions (like Amazon / WhatsApp) that have **ever** the same size without a button. Why change iOS 10 not the size of these extensions? –  Oct 08 '16 at 20:38
  • Or can I use the `maxSize` value of the `widgetActiveDisplayModeDidChange` method for that? –  Oct 08 '16 at 20:43
  • 1
    1. The compact height of widget is fixed by Apple (I don't remember the size tho, and I think it's the same for all iPhones, didn't look for iPads) You can easily get that height by setting a view with autolayout that take all the height, and display a label with its height inside 2. The apps on your phone that have a bigger size than the one define by Apple without any button are probably not updated (darkgray background). – Damien Oct 08 '16 at 20:47
  • WidgetActiveDisplayModeDidChange will tell you when the display mode change, it's the only way to change your UI to fit in compact and extended mode (you should use that if you allow extended) – Damien Oct 08 '16 at 20:49