在IOS10 Widget中以编程方式更改NCWidgetDisplayMode
我希望以编程方式更改今天扩展的高度。由于iOS10 SDSK引入了 NCWidgetDisplayMode
我试图用它来以编程方式更改我的 preferredContentSize
的高度。
I am looking to programmatically change the height of a today extension. As the iOS10 SDSK introduced NCWidgetDisplayMode
I am trying to use it to programmatically change the height of my preferredContentSize
.
我已实施 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)
}
}
当按下 UIButton
时,我想要小部件高度扩展:
I want the widget height to expand when a UIButton
is pressed :
@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 the today 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!
我试图在一个内部调用 widgetActiveDisplayModeDidChange
动画块:
I have tried to call widgetActiveDisplayModeDidChange
inside an 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
}
}
}
但我仍然得到没有活动动画块!
错误消息。有没有办法以编程方式在iOS10中扩展今天的扩展视图?
But I still get the No active animation block!
error message. Is there a way to programmatically expand a today extension view in iOS10 ?
在 iOS 10中
,显示更多/显示更少
按钮会自动提供在今天的扩展
中。因此小部件
的高度是通过 NCWidgetDisplayMode
自动处理的。您无需提供任何显式按钮来处理小部件的高度。
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
}
}
实施 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
}
}
In, iOS 8
和 iOS 9
,你需要显式处理小部件的高度。在 iOS 10
中,它不是必需的。
In, iOS 8
and iOS 9
, you need to explicitly handle widget's height. In iOS 10
, it is not required.
您可以参考今天的小工具 github.com/pgpt10/Today-Widgetrel =nofollow noreferrer> https://github.com/pgpt10/Today-Widget 在 iOS 8
, iOS 9
和 iOS 10
中实施。
You can refer to https://github.com/pgpt10/Today-Widget on Today's Widget
implementation in iOS 8
, iOS 9
and iOS 10
.