ios 如何设置 Today Widget Extension 的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24038087/
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-08-31 00:24:44  来源:igfitidea点击:

How to set the height of a Today Widget Extension?

iosios8xcode6ios-app-extension

提问by Gotschi

How can i change the height of my App's Today Extension in the Notification Center?

如何在通知中心更改应用程序今日扩展的高度?

I tried it with the Interface Builder and with Code, the Interface Builder Displays the View with height 600, but it's not applying this height on the device.

我尝试使用 Interface Builder 和 Code,Interface Builder 显示高度为 600 的视图,但它没有在设备上应用此高度。

It seems I can't get it bigger than some 80 pixels...

看来我不能让它大于大约 80 像素...

enter image description here

在此处输入图片说明

回答by Santa Claus

In your widget UIViewController.m(Objective-C):

在您的小部件UIViewController.m(Objective-C)中:

self.preferredContentSize = CGSizeMake(0, 200);

Will make your widget have a height of 200.

将使您的小部件具有 200 的高度。

Note that the width will have no affect on the view, as widgets must fit in the exact width of notification center, which is handled automagically.

请注意,宽度不会影响视图,因为小部件必须适合通知中心的确切宽度,这是自动处理的。

Also, if you want to animate changes in the height of your view, you can implement (Objective-C):

此外,如果您想对视图高度的变化进行动画处理,您可以实现 (Objective-C):

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

in your view controller using -animateAlongsideTransition:completion:

在您的视图控制器中使用 -animateAlongsideTransition:completion:

The answer was a bit hidden; you had to click around in the documentation sidebar to eventually find this fantastic document.

答案有点隐蔽。您必须在文档侧边栏中单击鼠标才能最终找到这个精彩的文档



Another way is to use auto-layout constraintsto constrain your view's height.

另一种方法是使用自动布局约束来约束视图的高度。

回答by lxt

Widgets have their heights adjusted by the system. If you have defined your height using constraints this will be automatically adjusted as required. If you're using explicit layout you can request a new height by modifying the preferredContentSizeof your widget.

小部件的高度由系统调整。如果您使用约束定义了您的高度,这将根据需要自动调整。如果您使用显式布局,则可以通过修改preferredContentSize小部件的来请求新高度。

Note that you have no guarantee notification center will respect your height request: it may be adjusted automatically, it may be adjusted but not to the exact height you want, or it may not be honored at all.

请注意,您无法保证通知中心会尊重您的身高要求:它可能会自动调整,可能会调整但未达到您想要的确切高度,或者可能根本不会兑现。

The best way to develop a widget is to use auto-layout constraints to set your height values, that way your widget will adapt to different heights with ease.

开发小部件的最佳方法是使用自动布局约束来设置您的高度值,这样您的小部件将轻松适应不同的高度。

回答by Lion

Since iOS 10 extension's height is 110 pixels. You should use new protocol method widgetActiveDisplayModeDidChange:withMaximumSize:to extend extension size (Objective-C):

由于 iOS 10 扩展的高度为 110 像素。您应该使用新的协议方法widgetActiveDisplayModeDidChange:withMaximumSize:来扩展扩展大小(Objective-C):

- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode
                         withMaximumSize:(CGSize)maxSize {

    if (activeDisplayMode == NCWidgetDisplayModeExpanded) {
        self.preferredContentSize = CGSizeMake(maxSize.width, 600.0);
    } else if (activeDisplayMode == NCWidgetDisplayModeCompact) {
        self.preferredContentSize = maxSize;
    }
}

Also you may need to call setWidgetLargestAvailableDisplayMode:on your extension context in today view controller's viewDidLoadmethod like this (Objective-C):

此外,您可能需要setWidgetLargestAvailableDisplayMode:在今天的视图控制器的viewDidLoad方法中调用您的扩展上下文,如下所示(Objective-C):

if ([self.extensionContext respondsToSelector:@selector(setWidgetLargestAvailableDisplayMode:)]) { // iOS 10+
    [self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeExpanded];
} else {
    self.preferredContentSize = CGSizeMake(0, 600.0); // iOS 10-
}

This thread may be helpful https://forums.developer.apple.com/thread/48930

此线程可能会有所帮助https://forums.developer.apple.com/thread/48930

回答by Ale? Kocur

The best way is of course Autolayout but by default there are margins which you can control like this

最好的方法当然是自动布局,但默认情况下,您可以像这样控制边距

func widgetMarginInsetsForProposedMarginInsets
    (defaultMarginInsets: UIEdgeInsets) -> (UIEdgeInsets) {
    return UIEdgeInsetsZero
}

回答by fAiSaL

There are two ways to display Today extension:

显示 Today 扩展有两种方式:

  1. Compact Mode (fixed height for Widget)
  2. Expand Mode (Variable height for Widget)
  1. 紧凑模式(小部件的固定高度)
  2. 扩展模式(小部件的可变高度)

Whatever code you do to change the height of extension in Compact mode will not make any difference. So you need to change the mode from compact to Expand Mode.

无论您使用什么代码来更改 Compact 模式下的扩展高度,都不会产生任何影响。因此您需要将模式从紧凑模式更改为扩展模式。

// 1. Load This in viewDidLoad:

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

// 2. Implement another widget protocol

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

You can refer WWDC for more updates about App extensions

您可以参考 WWDC 以获取有关 App 扩展的更多更新

回答by Vivek

Today widget default UIEdgeInsets defaultMarginInsets (UIEdgeInsets) defaultMarginInsets = (top = 0, left = 44, bottom = 39, right = 0)

今天小部件默认 UIEdgeInsets defaultMarginInsets (UIEdgeInsets) defaultMarginInsets = (top = 0, left = 44, bottom = 39, right = 0)

You should add this method

你应该添加这个方法

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets {
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 44, 0, 0);
return edgeInsets;}