Change NCWidgetDisplayMode programmatically in iOS 10 widgets

I want to programmatically change the height of today's extension. When the IOS10 SDSK introduced NCWidgetDisplayMode, I try to use it to programmatically change my height preferredContentSize.

I implemented widgetActiveDisplayModeDidChange:

@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: 280)
    }
}

I want the height of the widget to expand when clicked UIButton:

@IBAction func multiplybyonethousand (sender: AnyObject) {

    if self.extensionContext?.widgetActiveDisplayMode == NCWidgetDisplayMode.Compact {

        self.widgetActiveDisplayModeDidChange(.Expanded, withMaximumSize: CGSizeMake(0, 300))
    }
}

However, when I run my code, the height of today's extension does not change, and the console gives me the following error:

2016-11-05 14:24:29.425697 todayextension[28590:7222420] No active animation block!

I tried calling widgetActiveDisplayModeDidChangeinside the animation block:

@IBAction func multiplybyonethousand (sender: AnyObject) {
        if self.extensionContext?.widgetActiveDisplayMode == NCWidgetDisplayMode.Compact {
            UIView.animateWithDuration(0.2, delay: 0, options: .CurveLinear, animations: { () -> Void in
                self.widgetActiveDisplayModeDidChange(.Expanded, withMaximumSize: CGSizeMake(0, 300))

            }) { (completed) -> Void in
             //Do Stuff
            }
        }
}

But I still get the error message No active animation block!. Is there a way to programmatically expand an existing kind of extension in iOS10?

+4
source share
1

iOS 10 Show More/Show Less Today Extension. , widget NCWidgetDisplayMode. - .

override func viewDidLoad() {
    super.viewDidLoad()

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

NCWidgetProviding:

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

, iOS 8 iOS 9 . iOS 10 .

https://github.com/pgpt10/Today-Widget Today Widget iOS 8, iOS 9 iOS 10.

+9

Source: https://habr.com/ru/post/1659945/


All Articles