我正试图解决我今天的小部件的问题.按"显示更多"按钮后,它不会在iOS10上扩展.它的大小始终保持不变.
这是代码 TodayViewController.swift
import UIKit import NotificationCenter class TodayViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() extensionContext?.widgetLargestAvailableDisplayMode = .expanded } } extension TodayViewController: NCWidgetProviding { func widgetActiveDisplayModeDidChange(activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) { if activeDisplayMode == .expanded { preferredContentSize = CGSize(width: 0, height: 280) } else { preferredContentSize = maxSize } } }
有什么提示吗?
假设您使用Swift 3进行编码,请注意widgetActiveDisplayModeDidChange方法签名是:
可选的func widgetActiveDisplayModeDidChange(_ activeDisplayMode:NCWidgetDisplayMode,withMaximumSize maxSize:CGSize)
所以,它应该是:
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) { if activeDisplayMode == .expanded { preferredContentSize = CGSize(width: 0, height: 280) } else { preferredContentSize = maxSize } }
要仔细检查是否存在问题,可以在当前方法中添加断点,并检查它是否可访问.