Size problems by converting iOS 9 today extension

2019-07-13 06:10发布

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!

2条回答
戒情不戒烟
2楼-- · 2019-07-13 06:22

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

查看更多
Explosion°爆炸
3楼-- · 2019-07-13 06:29

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

查看更多
登录 后发表回答