Daniel Kilimnik
Daniel Kilimnik

Reputation: 57

Swift Today Extension preferredContentSize not resizing

so like the description says I am working on an iOS App and wanted to create a Today Extension Widget for it. My Problem is that I wanted to change the height to 200. After researching the only solution I found was to use the preferredContentSize atribute but this wasn't working for me.

I want to add a tableView to it and I wanted it to beshould be fully shown. I added the viewDidLoad method so you can see where the table is created and added.

override func viewDidLoad() {
    super.viewDidLoad()

    let myDefaults = UserDefaults(suiteName: "group.com.iOSApp")!
    let eventData = myDefaults.object(forKey: "events")

    if eventData != nil {
        shownEvents = NSKeyedUnarchiver.unarchiveObject(with: eventData as! Data) as! [Event]
    }

    eventTable = UITableView()
    eventTable.register(TodayViewCell.self, forCellReuseIdentifier: "cell")
    eventTable.separatorColor = UIColor.primary()
    view.addSubview(eventTable)
    eventTable.translatesAutoresizingMaskIntoConstraints = false

    var tempX = NSLayoutConstraint(item: eventTable, attribute: .leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
    var tempY = NSLayoutConstraint(item: eventTable, attribute: .top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .top, multiplier: 1, constant: 0) 
    NSLayoutConstraint.activate([tempX, tempY])

    tempX = NSLayoutConstraint(item: eventTable, attribute: .trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
    tempY = NSLayoutConstraint(item: eventTable, attribute: .bottom, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)   
    NSLayoutConstraint.activate([tempX, tempY])


    eventTable.delegate = self
    eventTable.dataSource = self

    eventTable.reloadData()

    preferredContentSize.height = 200


}

Upvotes: 3

Views: 1656

Answers (1)

Cruz
Cruz

Reputation: 2632

In iOS 10 setting preferredContentSize.height directly not work

after iOS 10, there are two type to show today extension

case 1 Show More (expanded type, can set custom height)

case 2 Show Less (compact type, has default hegiht)

enter image description here

What's new in iOS 10

so you should provide height for iOS 10 earlier versions and for iOS 10

if #available(iOSApplicationExtension 10.0, *) {
        //setup display mode (show more(.expended) or show less(.compact))
        extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    } else {
        // Fallback on earlier versions
        preferredContentSize.height = 200
    }

and shoulde implement for custom height

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {

@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
    switch activeDisplayMode {
    case .expanded: preferredContentSize.height = 200
    case .compact: preferredContentSize = maxSize
    }
}

Upvotes: 6

Related Questions