Ziel: Edge-to-Edge UICollectionView mit 2 Zellen auf allen iPhones.UICollectionView Edge to Edge Layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize = collectionView.bounds
let screenWidth = screenSize.width
print("Zhenya: \(screenWidth)") // prints correct 375, which is half of iPhone 6
let cellEdgeLength: CGFloat = screenWidth/2.0
return CGSize(width: cellEdgeLength, height: cellEdgeLength)
}
Auch
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
let flow = UICollectionViewFlowLayout()
flow.scrollDirection = UICollectionViewScrollDirection.vertical
flow.minimumInteritemSpacing = 0
flow.minimumLineSpacing = 0
collectionView.collectionViewLayout = flow
}
jedoch auf iPhone 6:
Sammlung Zelleigenschaften:
-Sammlung Ansicht Attribute:
Update: func für Steigung, die die richtige Breite tatsächlich bekommt: (an benutzerdefinierten UICollectionViewCell Klasse)
func addGradient() {
let gradient = CAGradientLayer()
gradient.frame = gradientView.bounds
let topColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1)
let botomColor = UIColor.clear
gradient.colors = [topColor.cgColor, botomColor.cgColor]
if gradientWasRemoved == false {
gradientView.layer.insertSublayer(gradient, at: 0)
} else if gradientWasRemoved == true {
self.addSubview(gradientView)
}
Update 2: Hinweis: Testen auf dem iPhone 7 Plus. Ich fand, dass UICollectionViewFlowLayout Zellgröße überschreibt in sizeForItemAtIndexPath:
crated (wie es scheint) Mit diesem Code:
let flow = UICollectionViewFlowLayout()
let screenSize = collectionView.bounds
let screenWidth = screenSize.width
let cellEdgeLength: CGFloat = screenWidth/2.0
flow.itemSize = CGSize(width: cellEdgeLength, height: cellEdgeLength)
flow.scrollDirection = UICollectionViewScrollDirection.vertical
flow.minimumInteritemSpacing = 0
flow.minimumLineSpacing = 0
collectionView.collectionViewLayout = flow
Ich habe dies:
Dann habe ich beschlossen, die manuelle Zellkantenlänge angeben (Hälfte von iPhone7Plust Breite = 207):
let cellSide: CGFloat = 207
flow.itemSize = CGSize(width: cellSide, height: cellSide)
flow.scrollDirection = UICollectionViewScrollDirection.vertical
flow.minimumInteritemSpacing = 0
flow.minimumLineSpacing = 0
collectionView.collectionViewLayout = flow
ich bekomme dies:
haben Sie versucht 'Return CGSize (Breite: (self.collectionView! .frame.width/2), Höhe: (self.collectionView! .frame.width/2)) 'stattdessen? frame = Position und Größe einer Ansicht unter Verwendung des Koordinatensystems der übergeordneten Ansicht bounds = Position und Größe einer Ansicht mit einem eigenen Koordinatensystem – mat
Hat nicht funktioniert. Was kann einige Hinweise geben - ich habe eine Verlaufsansicht in meiner Zelle, und es nimmt die richtige Breite. Ich werde meine OG-Post mit Code für den Farbverlauf aktualisieren. Vielleicht wird es helfen. –
vielleicht. Ich kann sehen, Sie haben Einschränkungen für diese Farbverlaufsansicht – mat