Ich versuche, eine Today Extension Sammlung mit der Angabe der Elemente pro Zeile von 3 Elemente und auch mit der Einstellung der Abschnitt Einsätze für mit 20 für oben, links, unten und Recht. Wenn ich dies in einer Single-View-Anwendung mache, ist alles wie erwartet, aber wenn ich programmatisch dasselbe für die Today-Erweiterung mache, sieht die Kollektionsansicht anders aus, besonders der Raum rechts und unten scheint nicht wie in der Single-View-Anwendung zu sein. Was ist der Grund, warum es diesen Unterschied gibt? Ich erwartete das gleiche Verhalten für die Today-Erweiterung wie in der Single-View-Anwendung.Heute Erweiterung mit UICollectionView unterschiedliches Verhalten im Vergleich zu Single View Anwendung
Um mein Problem besser zu verstehen, unten ist der Code, der Single View-Anwendung und die Heute-Erweiterung, die beide mit Screenshots:
Viewcontroller der Single View Anwendung:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var collectionView: UICollectionView!
let sectionInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
let itemsPerRow: CGFloat = 3
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = sectionInsets
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
collectionView.backgroundColor = .white
view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
cell.backgroundColor = .blue
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth/itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
}
Screenshots der Einzelansicht Anwendung:
TodayViewController der Heute-Erweiterung:
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var collectionView: UICollectionView!
let sectionInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
let itemsPerRow: CGFloat = 3
override func viewDidLoad() {
super.viewDidLoad()
extensionContext?.widgetLargestAvailableDisplayMode = .expanded
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = sectionInsets
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
collectionView.backgroundColor = .white
view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
cell.backgroundColor = .blue
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth/itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
// toggle height in case of more/less button event
if activeDisplayMode == .compact {
self.preferredContentSize = CGSize(width: 0, height: 110)
} else {
self.preferredContentSize = CGSize(width: 0, height: 220)
}
}
}
Screenshots der Heute-Erweiterung:
Ich glaube, dass die Berechnung von 'availableWidth' nicht funktioniert, da' view.frame.width' keine endgültige Dimension haben muss. Sehen Sie sich einfach den Wert in 'view.frame.width' an. – Sulthan
@Sultan Sie hatten Recht. Der Wert in viewDidLoad ist 375.0 und in viewDidAppear ist es 359.0 – ronatory