简体   繁体   中英

CollectionView showing black Screen

Hi I am trying to make a collectionView programmatically. I have changed its color and it still shows the default one. And the code does not really work. I am pretty sure its the problem in the appDelegate but can't figure it out . Here is my application method in appDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Create one
    window = UIWindow(frame: UIScreen.main.bounds)

    //Shows the window and makes it the key window.
    window?.makeKeyAndVisible()

    //Must give a layout to it by default
    let flowLayout = UICollectionViewFlowLayout()
    let customCollectionViewController = UICollectionViewController(collectionViewLayout: flowLayout)
    window?.rootViewController = UINavigationController(rootViewController: customCollectionViewController)
    return true
}

And Here is the CollectionViewController code

class CustomCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let identifier = "customCell"
override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.backgroundColor = UIColor.gray
    collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: identifier)
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
    return customCell
}



override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 100)
}

}

class CustomCell: UICollectionViewCell{
   override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

let nameLabel: UILabel = {
    let label = UILabel()
    label.text = "Hello World"

    label.translatesAutoresizingMaskIntoConstraints = false
    return label

}()

func setupViews(){
    backgroundColor = UIColor.white
    addSubview(nameLabel)

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|v0|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|v0|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

I am using Xcode 8.2.1

You aren't using yourCustomCollectionViewController, you are using UICollectionViewController.

Replace

    let customCollectionViewController = UICollectionViewController(collectionViewLayout: flowLayout)

with

    let customCollectionViewController = CustomCollectionViewController(collectionViewLayout: flowLayout)

Your constraints code in the cell is also causing a crash for me but if you just comment those lines you should see the collectionview appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM