我有今天的小部件,有时候"显示更多"不管用。此问题仅出现在iOS 11中,之前的版本(8,9,10)运行良好。我不明白我做错了什么以及为什么我不能扩展小部件。我已经调查了这个问题并发现了" WidgetPerformUpdate"改变后没有触发" PreferredContentSize"内部" WidgetActiveDisplayModeDidChange"这是因为" View.Bounds"更改" PreferredContentSize"。
后,小部件未更改以下是我的代码示例:
[Register ("TodayViewController")]
public class TodayViewController : UIViewController, INCWidgetProviding
{
private const int PrefferedHeight = 300;
private readonly ISettings _settings;
public TodayViewController(IntPtr handle) : base (handle)
{
_settings = AppContainer.Resolve<ISettings>();
}
static TodayViewController()
{
AppContainer.Container = (new iOSAppSetup()).CreateContainer();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
AddOpenApplicationEvent();
// Tell widget it can be expanded
ExtensionContext.SetWidgetLargestAvailableDisplayMode(NCWidgetDisplayMode.Expanded);
// Get the maximum size
var maxSize = ExtensionContext.GetWidgetMaximumSize(NCWidgetDisplayMode.Expanded);
PreferredContentSize = new CGSize(maxSize.Width, PrefferedHeight);
View.AddSubview(***view according to maxSize***);
}
private void AddOpenApplicationEvent()
{
var tapWidget = new UITapGestureRecognizer();
tapWidget.AddTarget(() =>
{
UIApplication.SharedApplication.OpenUrl(new NSUrl("test://"));
});
View.AddGestureRecognizer(tapWidget);
}
[Export("widgetPerformUpdateWithCompletionHandler:")]
public void WidgetPerformUpdate(Action<NCUpdateResult> completionHandler)
{
UpdateWidget();
switch (ExtensionContext.GetWidgetActiveDisplayMode())
{
case NCWidgetDisplayMode.Compact:
// changes according to compact mode
break;
case NCWidgetDisplayMode.Expanded:
// changes according to expanded mode
break;
}
completionHandler(NCUpdateResult.NewData);
}
[Export("widgetActiveDisplayModeDidChange:withMaximumSize:")]
public void WidgetActiveDisplayModeDidChange(NCWidgetDisplayMode activeDisplayMode, CGSize maxSize)
{
// Take action based on the display mode
switch (activeDisplayMode)
{
case NCWidgetDisplayMode.Compact:
PreferredContentSize = maxSize;
// changes according to compact mode
break;
case NCWidgetDisplayMode.Expanded:
PreferredContentSize = new CGSize(maxSize.Width, PrefferedHeight);
// changes according to expanded mode
break;
}
}
private void UpdateWidget()
{
// some updates
}
P.S。有时它通过小部件导航到应用程序后工作。
答案 0 :(得分:0)
在 iOS-11 中, Show More/Show Less
工作正常。
我已经创建了一个基本样本来描述这个:
class TodayViewController: UIViewController, NCWidgetProviding
{
override func viewDidLoad()
{
super.viewDidLoad()
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void))
{
completionHandler(NCUpdateResult.newData)
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize)
{
if activeDisplayMode == .expanded
{
preferredContentSize = CGSize(width: 0.0, height: 300.0)
}
else
{
preferredContentSize = maxSize
}
}
}