xcode 今日扩展:如何使用显示模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37995859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:57:26  来源:igfitidea点击:

Today Extension: How to work with display mode?

xcodeswiftexpandtoday-extensionios10

提问by Vladius001

Widgets now include the concept of display mode (represented by NCWidgetDisplayMode), which lets you describe how much content is available and allows users to choose a compact or expanded view.

小部件现在包括显示模式的概念(由NCWidgetDisplayMode表示),它可以让您描述有多少可用的内容,并允许用户选择紧凑或扩展的视图。

How to expand widget in ios 10.0? It doesn't work as in ios 9.

如何在 ios 10.0 中扩展小部件?它不像在 ios 9 中那样工作。

回答by Vladius001

Ok, i found right solution here.

好的,我在这里找到了正确的解决方案。

1) Set the display mode to NCWidgetDisplayMode.expandedfirst in viewDidLoad:

1) 将显示模式设置为NCWidgetDisplayMode.expandedfirst in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded
}

2) Implement new protocol method:

2) 实现新的协议方法:

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
    if (activeDisplayMode == NCWidgetDisplayMode.compact) {
        self.preferredContentSize = maxSize
    }
    else {
        //expanded
        self.preferredContentSize = CGSize(width: maxSize.width, height: 200)
    }
}

And it will work as official apps.

它将作为官方应用程序运行。

Image

图片

回答by user6716827

Here is a Objective-C one.

这是一个Objective-C。

- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode
                         withMaximumSize:(CGSize)maxSize
{
    if (activeDisplayMode == NCWidgetDisplayModeCompact) {
        self.preferredContentSize = maxSize;
    }
    else {
        self.preferredContentSize = CGSizeMake(0, 200);
    }
}