I have an extension I've taken down the bare bones, which is putting itself in incorrect states, where it'll say "Show Less" when it's collapsed.
This is happening in two circumstances
I also tried having another extension active (weather) with it expanded, and it always remains expanded once expanded, while my extension is collapsing and showing the wrong state.
This happens with and without the weather widget present.
When I put break points in the code, in step #1 ViewDidLoad is being called again.
Here's the code, I deleted everything bit by bit until this was all that was left and still causing the problem.
class TodayController: UICollectionViewController, NCWidgetProviding {
let reuseIdentifier = "TimeGridCell"
override func viewDidLoad() {
super.viewDidLoad()
self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
for: indexPath) as! ArtistTimeGridTableViewCell
cell.nameLabel.text = "blah blah"
return cell
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
self.preferredContentSize = (self.collectionView?.contentSize)!
} else if activeDisplayMode == .compact {
self.preferredContentSize = maxSize
}
}
}
Note that if I remove self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded
then I don't get the option to expand or collapse at all, so it's required.
Here's images of the problem. First image is correct, second one is incorrect.
Found another person talking about a problem with the button not working right, and their solution appears to work for me, although no good explanation what's actually wrong? Seems like this may be an OS bug?
iOS10 widget "Show more" "Show less" bug
Putting a delay on setting the content size prevents the issue
let deadlineTime = DispatchTime.now() + 0.2
if activeDisplayMode == .expanded {
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
self.preferredContentSize = (self.collectionView?.contentSize)!
}
} else if activeDisplayMode == .compact {
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
self.preferredContentSize = maxSize
}
}