Change NCWidgetDisplayMode programmatically in IOS10 Widget

ぐ巨炮叔叔 提交于 2019-12-04 08:13:40
PGDev

In iOS 10, Show More/Show Less button is automatically provided in the Today's Extension. So height of the widget is handled automatically through NCWidgetDisplayMode. You don't need to provide any explicit button for handling the widget's height.

override func viewDidLoad() {
    super.viewDidLoad()

    if #available(iOSApplicationExtension 10.0, *) {
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    }
}

Implement NCWidgetProviding protocol's method:

@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
    if activeDisplayMode == .expanded {
            preferredContentSize = CGSize(width: maxSize.width, height: 300)
    } else {
        preferredContentSize = maxSize
    }
}

In, iOS 8 and iOS 9, you need to explicitly handle widget's height. In iOS 10, it is not required.

You can refer to https://github.com/pgpt10/Today-Widget on Today's Widget implementation in iOS 8, iOS 9 and iOS 10.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!