Search code examples
swift3ios10xcode8today-extensionios10-today-widget

Swift 3 / iOS 10 Today View Widget


How to show "Show More" button in today widget (similar to news app as attached here)? I found this on Apple but there are some changes in swift 3 / iOS 10. This seems to be like something new in iOS 10.

enter image description here


Solution

  • This code did the trick of showing "Show More"

    override func viewDidLoad() {
          super.viewDidLoad()
    
        self.preferredContentSize = CGSize(width: 320, height: CGFloat(items.count)*121 + 44)
    
        if #available(iOSApplicationExtension 10.0, *) {
            self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
        } else {
            // Fallback on earlier versions
        }
    }
    
    // For iOS 10
    @available(iOS 10.0, *)
    @available(iOSApplicationExtension 10.0, *)
    func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
        self.preferredContentSize = (activeDisplayMode == .expanded) ? CGSize(width: 320, height: CGFloat(items.count)*121 + 44) : CGSize(width: maxSize.width, height: 110)
    }